Promise Basic in JavaScript

Level 4170 keystrokes

Promises handle asynchronous operations, resolving with a value or rejecting with an error.

JavaScript code

// Create a new Promise
// The executor function receives resolve and reject callbacks
1const promise = new Promise((resolve, reject) => {
// setTimeout simulates an async operation
// After 100ms, resolve with "Success!"
2 setTimeout(() => resolve("Success!"), 100);
3});
// .then() handles the resolved value
// This callback runs when the promise resolves
4promise.then(result => console.log(result));
// This runs immediately (before promise resolves)
5console.log("Waiting...");

Output

Waiting...
Success!
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.

  • (× 8
  • )× 8
  • .× 6
  • ;× 4
  • "× 4
  • =× 4
  • >× 3
  • ,× 2
  • !× 1
  • {× 1

More JavaScript examples at level 4

← JavaScript typing practice