Promise.all with Empty Arrays
What you'll learn: How Promise.all behaves when you pass it an empty array—a surprisingly simple edge case that's important to understand.
The Edge Case
You've learned that Promise.all waits for multiple promises to resolve. But what happens when you give it zero promises to wait for?
Promise.all([])
.then(result => {
console.log(result); // What happens here?
});
Immediate Resolution
When you pass an empty array to Promise.all, it resolves immediately with an empty array. There's no waiting, because there's nothing to wait for.
Promise.all([])
.then(result => {
console.log(result); // Logs: []
console.log('This runs right away!');
});
Think of it like organizing a group meeting: if nobody is invited, the meeting is "complete" the moment you schedule it—there's no waiting period.
Why This Makes Sense
This behavior is logically consistent: Promise.all returns an array of results from all promises. If there are zero promises, there are zero results, so an empty array is the correct answer.
This design also makes Promise.all safe to use with dynamic arrays without special checks:
function processItems(items) {
const promises = items.map(item => fetchData(item));
// Works fine even if items is empty!
return Promise.all(promises);
}
It Never Rejects
An empty Promise.all always resolves, never rejects—because there are no promises that could reject.
Key Takeaway: Promise.all([]) immediately resolves with an empty array [], making it safe to use with dynamic promise collections that might sometimes be empty.