1×
Become a member
to unlock all features
Autoplay

    Create a New Promise in JavaScript with the Promise Constructor

    javascriptJavaScript

    The Promise constructor is used to create a new Promise object. It receives a single function as a parameter (known as the executor function), which in turn receives the resolve and reject functions as parameters:

    jsconst promise = new Promise((resolve, reject) => {
      // Perform some operation, then call either resolve() or reject()
    });

    Within the body of the executor function, you can perform any operation — typically, an asynchronous one. You then either call resolve(value) or reject(reason), depending on the outcome of that operation, to fulfill or reject the promise.

    Note that the Promise object is rejected if an error is thrown within the body of the executor function. The return value of the executor function is ignored.