Code Logo

Concurrent with Promise.all()

Published at25 Jul 2026
JavaScript Functions Hard 0 views
Like0

Write a JavaScript function that takes an array of numbers, creates a delayed doubling Promise for each, and uses Promise.all() to wait for all of them to resolve, returning the array of doubled values.

Promise.all() is a static method that takes an iterable of Promises and returns a single Promise that resolves when all input Promises have resolved. The resolved value is an array of the individual results in the same order as the input. If any Promise rejects, Promise.all() immediately rejects with that error.

Unlike sequential .then() chaining where operations run one after another, Promise.all() runs all Promises concurrently. This is essential for performance when you have multiple independent async operations. The concurrency is not parallelism — JavaScript remains single-threaded, but I/O operations can overlap.

Time complexity is O(n) for creating n promises. The total wall-clock time is determined by the slowest promise, not the sum of all. The operations run concurrently, not sequentially. Space complexity is O(n) for the results array.

Edge cases include empty array (Promise.all resolves immediately with []), single promise, mixed fast and slow operations, and ensuring the results maintain input order.

Example Input & Output

Example 1
Input
[]
Output
[]
Explanation

Empty array

Example 2
Input
[10,20,30]
Output
[20,40,60]
Explanation

Larger numbers

Example 3
Input
[1,2,3]
Output
[2,4,6]
Explanation

Double all numbers

Example 4
Input
[0,1,0]
Output
[0,2,0]
Explanation

Mixed with zero

Example 5
Input
[5]
Output
[10]
Explanation

Single element

Algorithm Flow

Recommendation Algorithm Flow for Concurrent with Promise.all()
Recommendation Algorithm Flow for Concurrent with Promise.all()

Solution Approach

function delayedDouble(x) {
  return Promise.resolve(x * 2);
}

function solution(nums) {
  var promises = nums.map(function(x) {
    return delayedDouble(x);
  });
  return Promise.all(promises);
}

Best Answers

javascript - Approach 1
function delayedDouble(x) {
  return Promise.resolve(x * 2);
}

function solution(nums) {
  var promises = nums.map(function(x) {
    return delayedDouble(x);
  });
  return Promise.all(promises);
}