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 milliseconds1const delay = (ms, value) => new Promise(resolve => setTimeout(() => resolve(value), ms));2 // Async function to fetch all data3async function fetchAll() { // Promise.all runs all promises in parallel4 const results = await Promise.all([ // First promise resolves after 100ms5 delay(100, "First"), // Second promise resolves after 50ms6 delay(50, "Second"), // Third promise resolves after 75ms7 delay(75, "Third")8 ]); // Results keep the array order, not the finish order: [First, Second, Third]9 console.log(results);10}// Call the async function11fetchAll();Output
[ 'First', 'Second', 'Third' ]
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