Important Properties Of Promise Methods

Important Properties Of Promise Methods

Table of contents

No heading

No headings in the article.

  1. .then()

    • returns a value: p gets fulfilled with the returned value as its value.

    • doesn't return anything: p gets fulfilled with undefined.

    • 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 by then 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

  2. .catch()

    • The catch method is used for error handling in promise composition. Since it returns a Promise, 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(), or finally()), then the rejection event is surfaced by the host. In the browser, this results in an unhandledrejection 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 calls then() on the object upon which it was called, passing undefined and onRejected as arguments. The value of that call is directly returned. This is observable if you wrap the methods.

        catch(onRejected)
      
        catch((reason) => {
          // rejection handler
        })
      
  1. .finally()

    • The finally() method of a Promise 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 value 77, Promise.resolve(2).finally(() => 77) returns a promise eventually fulfilled with the value 2.

      • Similarly, unlike Promise.reject(3).then(() => {}, () => 88), which returns a promise eventually fulfilled with the value 88, Promise.reject(3).finally(() => 88) returns a promise eventually rejected with the reason 3.