DEV Community

Discussion on: Struggling with Promises? You Are Not Alone!

Collapse
 
peerreynders profile image
peerreynders

Just to add:

When a promise is no longer pending it is considered settled.

Promise.allSettled() - JavaScript | MDN

The Promise.allSettled() method returns a promise that resolves after all of the given promises have either fulfilled or rejected, with an array of objects that each describes the outcome of each promise.

favicon developer.mozilla.org

console.log('Vacuuming');
const promise = Promise.resolve().then(() => console.log('Picking up Milk'));
console.log('Washing the Dishes');
promise.then(() => console.log('Take a break'));
console.log('Done inside');
/* 
  Output:
  "Vacuuming"
  "Washing the Dishes"
  "Done inside"
  "Picking up Milk"
  "Take a break"
*/
Enter fullscreen mode Exit fullscreen mode

It's sometimes useful to remember that just because a promise value has settled doesn't mean that it will immediately get processed if something synchronous is currently being processed. One can then onto an already settled promise but the processing will still occur asynchronously.

Also recursively settling promises is the fastest way to starve the event loop.