Ever wanted to be a “Promises whizz”. There are a few tricks you can learn to become that through the use of some promise static methods

On your way to become a “JavaScript Superstar” and “Promises whizz”, you’ll need to master the concept of handling multiple promises at the same time. There are some methods that are provided by the language to do just that, but before we dwell on those methods let’s begin with understanding some basics of “states” and “fates” of promises.

States and Fates

Promises generally have 3 possible mutually exclusive states, they are:

  • fulfilled — A promise is “fulfilled” if promise.then(f) will call f as soon as possible
  • rejected — A promise is rejected if promise.then(undefined, r) will call r “as soon as possible.”
  • pending — A promise is pending if it is neither “fulfilled” nor “rejected”.

An umbrella term, commonly used, for the above would be “settled” where a promise is said to be “settled” if it is not pending (that is if it is either fulfilled or rejected).

Promises have two possible mutually exclusive fates, they are:

  • resolved — A promise is “resolved” if trying to resolve or reject it has no effect, that is the promise has been locked-in to either follow another promise (“thenable” value) or has been “fulfilled” or “rejected”.
  • unresolved — A promise is “unresolved” if it is not resolved, that is if trying to resolve or reject it will have an impact on the promise.

Now that we have covered the concept of states and fates, we should now be able to understand promise concepts with common terminology. Let’s move on to the gist of the article.




#javascript #programming #developer

JavaScript Promises: Promise.all vs Promise.allSettled vs Promise.race vs Promise.any
2.50 GEEK