Skip to content
dreamcode
dreamcode
Map
Promises
Lesson 32 of 48
+15 XP on finish
JS ASYNCChapter 6 · JS Async and Errors

Promises

A Promise stands for a value that is not ready yet. It starts pending, then either fulfills with a value or rejects with an error. .then(fn) runs when it fulfills, .catch(fn) when it rejects, and .finally(fn) either way. Promise.all waits for several promises and fails as soon as any one rejects; Promise.allSettled waits for every one of them no matter what.

Worked example

How it reads

  • Each .then receives what the previous step returned
  • Promise.all keeps the results in the order you passed the promises
  • The rejection is caught first: it needs no timer, so it settles right away
Common mistakes
  • A promise chain with no .catch hides errors. Every chain should end with a catch, or be awaited inside try...catch.
Cloud tip: Return the next promise from inside .then to keep a chain flat instead of nesting callbacks.
index.js
JAVASCRIPT
real JavaScript, runs in your browser
Console
Run your code to see its output here.
YOUR TURN

Also call loadStar("") and handle its rejection with .catch, logging failed: no name.

Press Run to check your work.