DEV Community

Cover image for Important concepts of ES6 Promises
Kenneth Lum
Kenneth Lum

Posted on • Updated on

Important concepts of ES6 Promises

  1. There is one way to resolve a promise, which is to call the resolve function
  2. There are two ways to reject a promise, which is to call the reject function, or to throw an exception
  3. Note that when a new promise is created, the executor function immediately runs. The executor function in new Promise((resolve, reject) => { ... }) is the part (resolve, reject) => { ... }
  4. a .catch(fn) is the same as a .then(null, fn)
  5. a .then() returns a promise
  6. We can reject() with no reason at all, or with with an error, or other values.
  7. We can create a resolved promise by Promise.resolve(123), and create a rejected promise by Promise.reject()
  8. it is usually easier to think of a promise as a "thenable", and a "catchable", which is, to pre-arrange what happens when it is resolved or rejected (settled).
  9. The then() handler and the catch() or finally() handler are like the "listener" as in the observer pattern, but for the observer patter, the observer can register "too late" and don't get notified for something, but for promises, there is no "too late" -- they will get called. One other thing is that observers can be notified multiple times, but the promise handlers will be only called once
  10. settled means either resolved or rejected
  11. .race() is a competition for any resolve or reject
  12. .any() is a competition for any resolve
  13. .all() is for all to resolve or reject
  14. .allSettled() is for all to resolve (so any reject will reject it)
  15. the above 4 methods all return a new promise
  16. if we chain a series of then(), such as fetch().then().then().then().catch(), all the then() and catch() run immediately, setting up the functions to call when appropriate. The .then() and .catch() and .finally() all run immediately -- it is the handlers that get run later on.
  17. fetch() returns a promise, and so does axios.get(" ") or axios(" ")
  18. finally() is very close to then(fn1, fn1), except there is no resolved value passed in
  19. we can do fetch.then().then().then().catch().then().then().then().catch().then(), any reject or error thrown in the series of then() handlers would cause it to go immediately to the next catch() handler, and it will go again, one by one, if there is / are then() handlers, until any reject or error thrown, ... it is the same principle as above.
  20. the catch() handler can reject or throw error too, and in this case it would cause the next catch() handler to run
  21. so all .then(), .catch(), and .finally() return a promise
  22. If it is difficult to think about the .then().then().catch().then().then() situations, we can just think about aPromise .then(handleResolve1, handleReject1) .then(handleResolve2, handleReject2) .then(handleResolve3, handleReject3); with the null filled in, instead of actually providing a handler.
  23. if we have fetch().then().then().then().then().then(), remember the .then() returns a promise, so let's take one promise out, let's call it promise number 3. If that handler returns a promise X, then this promise number 3 is a promise that wraps around the promise X. Promise X resolves, promise number 3 resolves to the same value. Promise X rejects, promise number 3 rejects also.
  24. same as above, but if the handler returns something that is not a promise, promise number 3 resolves with that value. Note that if the handler does not return anything, then it is the same as returning undefined, so that means promise number 3 resolves with undefined
  25. It follows from above that, if we have aPromise.then().catch().catch(), the second catch handler will only run if the first catch handler returns a rejected promise or throws an error.
  26. to provide the then(), catch(), and finally() handler, whatever we give it, we in general don't want to immediately run something, but to provide a function. If we do immediately run something, we in general want to run something that gives back a function (to be run later) -- the key is something to be run later

Comments welcome. Trying to summarize some concepts of promises.

Top comments (0)