Promise.all in JavaScript

Level 4209 keystrokes

Promise.all() waits for all promises to resolve and returns their results as an array.

JavaScript code

// Create three resolved promises
1const p1 = Promise.resolve(1);
2const p2 = Promise.resolve(2);
3const p3 = Promise.resolve(3);
// Promise.all takes an array of promises
// Resolves when ALL promises resolve
// Rejects if ANY promise rejects
4Promise.all([p1, p2, p3])
5 .then(results => {
// results is an array of all resolved values [1, 2, 3]
6 console.log(results);
// Sum all values: 1 + 2 + 3 = 6
7 console.log(results.reduce((a, b) => a + b));
8 });

Output

[ 1, 2, 3 ]
6
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.

  • (× 9
  • )× 9
  • .× 8
  • ;× 6
  • =× 5
  • ,× 3
  • >× 2
  • [× 1
  • ]× 1
  • {× 1

More JavaScript examples at level 4

← JavaScript typing practice