Important concepts of ES6 Promises
Tags: promise, es6, webdev, javascript
Published: 2021-03-15 16:18:54

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
2. 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) => { ... }`
3. a `.catch(fn)` is the same as a `.then(null, fn)`
4. a `.then()` returns a promise
4. We can `reject()` with no reason at all, or with with an error, or other values.
4. We can create a resolved promise by `Promise.resolve(123)`, and create a rejected promise by `Promise.reject()`
5. 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).
5. 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
6. settled means either resolved or rejected
7. `.race()` is a competition for any resolve or reject
8. `.any()` is a competition for any resolve
9. `.all()` is for all to resolve or reject
10. `.allSettled()` is for all to resolve (so any reject will reject it)
11. the above 4 methods all return a new promise
12. 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.
12. `fetch()` returns a promise, and so does `axios.get("   ")` or `axios("   ")`
13. `finally()` is very close to `then(fn1, fn1)`, except there is no resolved value passed in
14. 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.
15. the `catch()` handler can reject or throw error too, and in this case it would cause the next `catch()` handler to run
16. so all `.then()`, `.catch()`, and `.finally()` return a promise
16. 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.
17. 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.
18. 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`
18. 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.
19. 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.