Welcome to Professional Knowledge & Training Center ProKTC

Promise in ES6 : An Introduction

Neeraj Jio
Over 10 years of experience in web tec.
Published on: 01 May 2019

Introduction

In modern JavaScript, we have promise objects. Promises can be an alternative to callbacks for asynchronous APIs. Instead of passing a callback as an argument and handling the error in the same place, a promise object allows us to handle success and error cases separately and it also allows us to chain multiple asynchronous calls instead of nesting them.

Lets understand this.
 

var asyncAdd = (a, b) => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        if (typeof a === 'number' && typeof b === 'number') {
          resolve(a + b);
        } else {
          reject('Arguments must be numbers');
        }
      }, 1500);
    });
  };

 

  5.00 Rating        2 like       4 View
Is this Article Helpful?      

Enter your Comment