Promise.all Parallel in JavaScript

Level 5256 keystrokes

Promise.all executes multiple promises in parallel and waits for all of them to resolve.

JavaScript code

// Helper that returns a promise resolving to value after ms milliseconds
1const delay = (ms, value) => new Promise(resolve => setTimeout(() => resolve(value), ms));
2
// Async function to fetch all data
3async function fetchAll() {
// Promise.all runs all promises in parallel
4 const results = await Promise.all([
// First promise resolves after 100ms
5 delay(100, "First"),
// Second promise resolves after 50ms
6 delay(50, "Second"),
// Third promise resolves after 75ms
7 delay(75, "Third")
8 ]);
// Results keep the array order, not the finish order: [First, Second, Third]
9 console.log(results);
10}
// Call the async function
11fetchAll();

Output

[ 'First', 'Second', 'Third' ]
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this JavaScript code and how often they appear.

  • (× 12
  • )× 12
  • ,× 7
  • "× 6
  • =× 5
  • ;× 4
  • >× 3
  • .× 2
  • [× 1
  • ]× 1

More JavaScript examples at level 5

← JavaScript typing practice