Promise Creation in JavaScript

Level 4247 keystrokes

This creates a Promise that resolves on success and logs the result.

JavaScript code

// Create a new Promise with resolve and reject functions
1const promise = new Promise((resolve, reject) => {
// Simulate a condition
2 const success = true;
3 if (success) {
// resolve() means the operation completed successfully
4 resolve("Operation succeeded!");
5 } else {
// reject() means the operation failed
6 reject("Operation failed!");
7 }
8});
9
// .then() handles successful resolution
// .catch() handles rejection/errors
10promise
11 .then(result => console.log(result))
12 .catch(error => console.log(error));

Output

Operation succeeded!
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
  • ;× 5
  • =× 5
  • .× 4
  • "× 4
  • {× 3
  • }× 3
  • >× 3
  • !× 2

More JavaScript examples at level 4

← JavaScript typing practice