Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

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.