Promise chain in JavaScript
Level 4193 keystrokes
Chains three then calls on a promise resolved with 10. Each then logs the value it receives and returns double it for the next one, so 10, 20 and 40 are printed.
JavaScript code
// Simulates a data fetch: returns a promise already resolved with 101const fetchData = () => Promise.resolve(10);2 // Starts the chain; each then runs after the previous one finishes3fetchData() // The first then receives the resolved value 104 .then(num => { // Prints 105 console.log(num); // The return value 20 becomes the input of the next then6 return num * 2;7 }) // The second then receives 208 .then(num => { // Prints 209 console.log(num); // Doubles again and passes 40 on10 return num * 2;11 }) // The last then receives 40 and prints it12 .then(num => console.log(num));Output
10 20 40
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.× 7;× 6=× 5>× 4{× 2}× 2*× 2