Skip to content

Instantly share code, notes, and snippets.

@basarat
Created March 13, 2023 06:02
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save basarat/795d43b48ea29a43960076693db5440c to your computer and use it in GitHub Desktop.
Save basarat/795d43b48ea29a43960076693db5440c to your computer and use it in GitHub Desktop.
Promise Fulfilled vs Resolved

Notice that we’ve been using the term, fulfilled instead of resolved, and the reason is that a promise can be resolved to another promise and which point its fate becomes dependent on the other promise. If the other promise gets fulfilled our promise gets fulfilled, if the other promise is rejected, our promise gets rejected.

Both alpha and beta are resolved, but they settle to different fates. Alpha gets fulfilled, Beta gets rejected.

const delayFulfill =
  () => new Promise(
    res => setTimeout(() => res('fulfilled'), 1000)
  );
//p
const delayReject =
  () => new Promise(
    (_, rej) => setTimeout(() => rej(new Error('rejected')), 1000)
  );
//p
const alpha = Promise.resolve(delayFulfill());
//p
const beta = Promise.resolve(delayReject());
//p
alpha
  .then(v => console.log(v))
  .catch(e => console.log(e.message));
//p
beta
  .then(v => console.log(v))
  .catch(e => console.log(e.message));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment