Javascript Promises

Promises in real-life experience is a trust between two or more persons and an assurance that a particular thing will surely happen.

In javascript, a Promise is an object which ensures to produce a single value in the future (when required). It is used for managing and tackling asynchronous operations.

There are two possible differences between Promise and Event Handlers:-

  • A Promise can never fail or succeed twice or more. This can happen only once.
  • A Promise can neither switch from success to failure, or failure to success. If a Promise has either succeeded or failed, and after sometime, if any success/failure callback is added, the correct callback will be invoked, no matter the event happened earlier.

Creating a Promise

To create a promise in JavaScript, we use the Promise constructor:-

Input:-
<script>
	   let completed = true;
    let learn = new Promise(function (resolve, reject) {
       setTimeout(() => {
          if (completed) {
             resolve("I have completed learning.");
          } else {
             reject("I haven't completed learning yet.");
      }
   }, 3 * 1000);
</script> 

Output(before 3 seconds):-

Output(after 3 seconds):-

Now, we see that the promise starts with the pending state with the value is undefined. The promise value will be returned later once the promise is completed.

After about 3 seconds, type the learn in the console window, we will see that the state of the promise becomes resolved and the promise value is the string that we passed to the resolve() function.

So calling the resolve() function moves the promise object to the fulfilled state.

If the promise reaches fulfilled state or rejected state, the promise is resolved.


Terminology used in Promise

A promise can be present in one of the below mentioned state:-

  • Pending: The pending promise is neither rejected nor fulfilled yet.
  • Fulfilled: The related promise action is fulfilled successfully.
  • Rejected: The related promise action is failed to be fulfilled.
  • Settled: Either the action is fulfilled or rejected.

So, a promise represents the completion of an asynchronous operation with its result. It can be either signify successful completion of the promise, or its failure, but eventually completed.

It uses a then() which is executed only after the completion of the promise resolve.


Promises of Promise

JavaScript Promise promises that:-

  • Unless the current execution of the js event loop is not completed (success or failure), callbacks will never be called before it.
  • Even if the callbacks with then() are present, but they will be called only after the execution of the asynchronous operations completely.
  • When multiple callbacks can be included by invoking then() many times, each of them will be executed in a chain, that means one after the other, following the sequence in which they were inserted.

Methods in Promise

The functions of Promise are executable almost on every trending web browsers such as Mozilla, Chrome, Opera, etc. Following is the methods list:-

Method NameSummary
Promise.resolve(promise)Returns promise only if promise.constructor==Promise.
Promise.resolve(thenable)Makes a new promise from thenable containing then().
Promise.resolve(obj)Makes a promise resolved for an object.
Promise.reject(obj)Makes a promise rejection for the object.
Promise.all(array)Makes a promise resolved when each item in an array is fulfilled or rejects when items in the array are not fulfilled.
Promise.race(array)If any item in the array is fulfilled as soon, it resolves the promise, or if any item is rejected as soon, it rejects the promise.