View on GitHub

marymafa.github.io


layout: post title: Promise date: 2018-05-31 —

Promise

Is an object that represents the eventual completion (or failure) of an asynchronous operation, and its resulting value. The promise can only be fulfilled or rejected once.

A promise itself has one of the following three states:

  1. Pending - until a promise is fulfilled it is pending states.

  2. Fulfilled- When the first handler is called promise is considered fulfilled with the value passed to that handler.

  3. Rejected- if the handlers are called the promise is considered rejected with the value passed to that handler.

The handler can be attached to the promise using .then() method. This function or method takes a function that will be passed the resolved value of the promise once it is fulfilled. the resolution calls the future promise when its ready and return it. the reject can reject the promise when it can’t resolve the future promise. The Await operation is used to wait for a promise, it can only be used inside an async function.