Promises

Introduction to promises in JavaScript for asynchronous programming

Background

Callback functions can be used to do tasks after the execution of asynchronous functions. However, callback function become tedious when designing a program with a series of sequential asynchronous functions. It results in code congestion.

To run asynchronous functions sequentially, nesting callbacks lead to “callback hell,” also known as the “pyramid of doom!” To avoid a doom pyramid that grows with every successive asynchronous function, use promises.

Introduction to promises

A promise is a class meant to neatly execute asynchronous functions. It acts as a wrapper to execute asynchronous functions with a callback by providing a nicer syntax. You can create promises with or without asynchronous functions. Let’s see the syntax.

Syntax of promises

Promises are objects that take a single argument. The function is as follows.

var promise = new Promise(func);

The function func passed as an argument takes two arguments. They are both here.

  • Resolve function: It is the first argument of the function func and is invoked, if it works as intended. The argument of the resolve function is given the return value.

  • Reject function: It is the second argument of the function func and is invoked if something fails. It is usually used for error handling. The argument of the reject function would be given the return value, which usually is an error object Error.

The function is defined as follows.

var func = function (resolve, reject){
  // add tasks synchronous or asynchronous
  if(/*Everything works as expected */){
    resolve(/* return value */);
  } else {
    reject(/* return value */);
  }
}

We can provide any number of arguments to resolve and reject functions. Once we create the promise, we can call the then method to invoke the function inside the promise.

then method

The then method adds callbacks or tasks that must be executed after the function inside the promise completes. The syntax is as ...