Promise Chaining in JavaScript

Level 4148 keystrokes

Promise chaining allows sequential asynchronous operations by returning values from then().

JavaScript code

// Promise.resolve(1) creates a resolved promise with value 1
1Promise.resolve(1)
// First then: 1 + 1 = 2
2 .then(x => x + 1)
// Second then: 2 * 2 = 4
3 .then(x => x * 2)
// Third then: log the result
4 .then(x => console.log(x));
// Another chain starting with 10 (it has fewer then steps, so 15 is printed before 4)
5Promise.resolve(10)
// 10 + 5 = 15
6 .then(x => x + 5)
// Log 15
7 .then(x => console.log(x));

Output

15
4
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
  • )× 9
  • =× 5
  • >× 5
  • ;× 2
  • +× 2
  • *× 1

More JavaScript examples at level 4

← JavaScript typing practice