Important Properties Of Promise Methods

.then()
returns a value:
pgets fulfilled with the returned value as its value.doesn't return anything:
pgets fulfilled withundefined.throws an error:
pgets rejected with the thrown error as its value.returns an already fulfilled promise:
pgets fulfilled with that promise's value as its value.returns an already rejected promise:
pgets rejected with that promise's value as its value.returns another pending promise: the fulfillment/rejection of the promise returned by
thenwill be subsequent to the resolution/rejection of the promise returned by the handler. Also, the resolved value of the promise returned bythenwill 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
catchmethod 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 anunhandledrejectionevent.If a handler is attached to a rejected promise whose rejection has already caused an unhandled rejection event, then another
rejectionhandledevent is fired.catch()internally callsthen()on the object upon which it was called, passingundefinedandonRejectedas 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 aPromiseobject schedules a function to be called when the promise is settled (either fulfilled or rejected).It immediately returns an equivalent
Promiseobject, 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
onFinallycallback 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.