Async Await Pattern in JavaScript
Level 5275 keystrokes
Async/await provides cleaner syntax for working with promises and asynchronous code.
JavaScript code
// Helper function that returns a promise that resolves after ms milliseconds1const delay = ms => new Promise(resolve => setTimeout(resolve, ms));2 // async function allows using await inside3async function process() { // Print "Start" immediately4 console.log("Start"); // await pauses execution until promise resolves5 await delay(100); // This runs after 100ms6 console.log("After 100ms"); // Wait another 100ms7 await delay(100); // This runs after total 200ms8 console.log("After 200ms"); // Return value becomes the resolved value9 return "Done";10}11 // Async functions return promises// .then() receives the returned value12process().then(result => console.log(result));Output
Start After 100ms After 200ms Done
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.
(× 11)× 11;× 8"× 8.× 5=× 4>× 3,× 1{× 1}× 1