Table of contents
No headings in the article.
.then()
returns a value:
p
gets fulfilled with the returned value as its value.doesn't return anything:
p
gets fulfilled withundefined
.throws an error:
p
gets rejected with the thrown error as its value.returns an already fulfilled promise:
p
gets fulfilled with that promise's value as its value.returns an already rejected promise:
p
gets rejected with that promise's value as its value.returns another pending promise: the fulfillment/rejection of the promise returned by
then
will be subsequent to the resolution/rejection of the promise returned by the handler. Also, the resolved value of the promise returned bythen
will be the same as the resolved value of the promise returned by the handler..then() takes in two callback functions depending on the state of the promise the corresponding callback function is called ie if the unfulfilled callback is called when the state of the promise changes from pending to resolved and the onrejected callback is called when the state of the promise changes from pending to rejected
The callback functions are optional parameters
Returns a new Promise always
.catch()
The
catch
method is used for error handling in promise composition. Since it returns aPromise
, it can be chained in the same way as its sister method,then()
.If a promise becomes rejected, and there are no rejection handlers to call (a handler can be attached through any of
then()
,catch()
, orfinally()
), then the rejection event is surfaced by the host. In the browser, this results in anunhandledrejection
event.If a handler is attached to a rejected promise whose rejection has already caused an unhandled rejection event, then another
rejectionhandled
event is fired.catch()
internally callsthen()
on the object upon which it was called, passingundefined
andonRejected
as arguments. The value of that call is directly returned. This is observable if you wrap the methods.catch(onRejected) catch((reason) => { // rejection handler })
.finally()
The
finally()
method of aPromise
object schedules a function to be called when the promise is settled (either fulfilled or rejected).It immediately returns an equivalent
Promise
object, allowing you to chain calls to other promise methods.When creating a function inline, you can pass it once, instead of being forced to either declare it twice, or create a variable for it.
The
onFinally
callback does not receive any argument. This use case is for precisely when you do not care about the rejection reason or the fulfillment value, and so there's no need to provide it.The callback function passed inside the finally does not take in any parameters.
A
finally()
call is usually transparent and does not change the eventual state of the original promise. So for example:Unlike
Promise.resolve(2).then(() => 77, () => {})
, which returns a promise eventually fulfilled with the value77
,Promise.resolve(2).finally(() => 77)
returns a promise eventually fulfilled with the value2
.Similarly, unlike
Promise.reject(3).then(() => {}, () => 88)
, which returns a promise eventually fulfilled with the value88
,Promise.reject(3).finally(() => 88)
returns a promise eventually rejected with the reason3
.