- There is one way to resolve a promise, which is to call the resolve function
- There are two ways to reject a promise, which is to call the reject function, or to throw an exception
- 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) => { ... }
- a
.catch(fn)
is the same as a.then(null, fn)
- a
.then()
returns a promise - We can
reject()
with no reason at all, or with with an error, or other values. - We can create a resolved promise by
Promise.resolve(123)
, and create a rejected promise byPromise.reject()
- 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).
- The
then()
handler and thecatch()
orfinally()
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 - settled means either resolved or rejected
-
.race()
is a competition for any resolve or reject -
.any()
is a competition for any resolve -
.all()
is for all to resolve or reject -
.allSettled()
is for all to resolve (so any reject will reject it) - the above 4 methods all return a new promise
- if we chain a series of
then()
, such asfetch().then().then().then().catch()
, all thethen()
andcatch()
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. -
fetch()
returns a promise, and so doesaxios.get(" ")
oraxios(" ")
-
finally()
is very close tothen(fn1, fn1)
, except there is no resolved value passed in - we can do
fetch.then().then().then().catch().then().then().then().catch().then()
, any reject or error thrown in the series ofthen()
handlers would cause it to go immediately to the nextcatch()
handler, and it will go again, one by one, if there is / arethen()
handlers, until any reject or error thrown, ... it is the same principle as above. - the
catch()
handler can reject or throw error too, and in this case it would cause the nextcatch()
handler to run - so all
.then()
,.catch()
, and.finally()
return a promise - If it is difficult to think about the
.then().then().catch().then().then()
situations, we can just think aboutaPromise .then(handleResolve1, handleReject1) .then(handleResolve2, handleReject2) .then(handleResolve3, handleReject3);
with thenull
filled in, instead of actually providing a handler. - 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. - 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 withundefined
- 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. - to provide the
then()
,catch()
, andfinally()
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)