JS Promise (Part 1 - Basics)

One of the good Features in ES6 is Promises Object and their useful methods and they are called software abstraction helps to works smoothly with asynchronous operations. Promise API followed Promises/A+ Specification prior to this, there was Promise/A.

What is Promises/A and Promises/A+ ?

  1. Both are specification for open standard but Javascript currently uses Promise/A+.
  2. The main three reason for using Promise/A+ is Three different states, Value for Fulfilment and for Rejection and thenable object. for more detail see Differences from Promises/A
  3. These Promise/A+ organisation occasionally revise and address the corner cases in to new Specification.

Difference between Promise/A and /A+?

  1. Rejection / Fulfilment method returns thenable object.
  2. Rejection triggers catch method, if errors thrown.

In short, running continuation-passing style. Let me shoot few bullets with simple diagram on Promises below.

  1. It’s a placeholder for the eventual results of a deferred (and possibly asynchronous) computation.
  2. It’s defined as an Object used for future result of an asynchronous computations.
  3. It represent a single asynchronous operation and gives a proxy until you get an actual value.
  4. Allows you to add handler to asynchronous of success or failure conditional actions.
  5. Return a promise instead of final value.
  6. It implements observer pattern (maintains a dependents and notifies them automatically of any state changes but can cause memory leaks, known as the lapsed listener problem) and its chain-able.
  7. It comes with integrated error handling and handles automatically fulfilled with value or reject with reason.

Why Promises?

  1. Promises mainly to solve Callback Hell (heavy nested callback code) and Pyramid of doom Problem.
  2. Promises helps to group your asynchronous in a efficient way.
  3. Javascript is not a new candidate for adapting Promises/A+ Specification. its been already included by other famous programming languages like Java (sample), Scala (sample), Python (sample) and Clojure (sample).

Promise States

There are three states which are below.

  1. pending: Initial Case where promise instantiated.
  2. fulfilled: Success Case which means promise resolved.
  3. rejected: Failure Case which means promise rejected.

Promise Methods

There are six methods which are below.

  1. Promise.all([promise1, promise2, …]);
  2. Promise.race([promise1, promise2, …]);
  3. Promise.reject(value);
  4. Promise.resolve(value);
  5. Promise.catch(onRejection);
  6. Promise.then(onFulFillment, onRejection);

Promise Iterable Methods

Below are the two Methods which accepts array of promises objects.

What is executor function ?

  1. Executor Functions are Parameter for Promise Constructor which holds Resolve and Reject Callbacks.
  2. It is executed immediately by the Promise implementation which provides the resolve and reject functions.
  3. It’s Triggered before the Promise constructor even returns the created object.
  4. The Resolve and Reject functions are bound to the promise to fulfill or reject.
  5. It’s expected to initiate some asynchronous work and then, once that completes, call either the resolve or reject.

What is the value of Promise.length?

It will be always one (1) due to number of constructor arguments.

Are You Tired with above Theories?

No Worries. Lets see quick and interesting questions with solutions from simple to advanced level !

How to create / instantiate the Promise?

Below is the Code Snippet for Simple Promise Instantiation.

How Promise methods can be overridden ?

Below are the two methods can be prototyped (overridden).
1. Promise.prototype.catch();
2. Promise.prototype.then();

How to do error handling in Promise ?

There are two possible ways to handle errors in Promise. Defining then method with second parameter which is onRejection callback or defining catch method.

How to detect whether Promise rejected ?

Promise triggers .catch or onRejection callback of .then method depends upon the precedence of definition.

How to combine multiple Promises in to one ?

Use `Promise.all` Method which combines multiple promise in to one.

What is Promise Racing ?

As everyone aware, Racing is competition of speed. Same thing applies to Promise API. Use `Promise.race` Method which can help to evaluate which is fast

What Promise.all() return for invalid parameter ?

Promise.all method accept array of objects which is promises in nature. Let’s see How promise.all behaves for various invalid parameters.

Why not Promise.race() with invalid parameter ?

Promise.race method also similar to all method accept array of objects. Let’s see How promise.race behaves for various invalid parameters.

What’s the main difference between callback-based functions and promise-based functions ?

  1. You don’t have to write error conditions `if (err) return callback(err)`.
  2. Callback need to be invoked the right callback immediately whereas Promise can be return as promise object and invoke later.
  3. In Promise, Anytime, When `.then()` method returned a promise and can be “unpacked” it to get to an actual value.
  4. In Callback, We must explicitly handle the error and we can’t deal with that error there.
  5. In Callback, We must call the passed callback to pass that error.

In short, Promise is better than callback for future asynchronous operation.

How many ways to resolve the Promise ?

There are two ways, with and without instantiation.

How to resolve with hard-coded value to Promise?

Sometimes, you will have a value already in place and you want to convert that into a true Promise. Promise.resolve() Call should be right choice.

How to reject with hard-coded value to Promise?

Same here for error. Use Promise.reject() Call.

Yes, You are done and you are in the last lines of this Article but this is not end of Promise Story and there is few more parts soon, stay tuned !

Wait is over, Have a look at next Article, Part 2 — Q.js, When.js and RSVP.js

Full Stack / Polyglot / React Developer ★ Redux ★ AWS ★ React Native ★ Flutter ★ Tech Writer ★ Open Source Enthusiast — webslate.io

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store