All tutorials
Advanced

Promises

An object representing the future result of an async operation.

1. Explanation

A Promise is in one of three states: pending, fulfilled, or rejected. Use .then(onFulfilled) and .catch(onRejected) to handle the outcome. Create one with new Promise((resolve, reject) => ...). Modern code prefers async/await on top of Promises.

2. Example

Try it yourself
Console
Click Run to see output.

3. Expected output

output
✅ done
settled

4. Common mistakes

  • Forgetting to return the inner promise inside .then — breaks chaining
  • Not adding a .catch — unhandled promise rejections crash modern Node
  • Mixing Promises and callbacks for the same API

5. Practice questions

  1. Wrap setTimeout in a promise-returning 'wait(ms)' helper
  2. Use Promise.all to wait for three timers
  3. Chain three .then steps that each transform the value
→ Try more practice problems