ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Constructing a Promise Object
    Study/JavaScript 2020. 5. 15. 17:13

    Constructing a Promise Object

    Let’s construct a promise! To create a new Promise object, we use the new keyword and the Promise constructor method:

     

    const executorFunction = (resolve, reject) => { };
    const myFirstPromise = new Promise(executorFunction);

     

     

    The Promise constructor method takes a function parameter called the executor function which runs automatically when the constructor is called. The executor function generally starts an asynchronous operation and dictates how the promise should be settled.

    The executor function has two function parameters, usually referred to as the resolve() and reject() functions. The resolve() and reject() functions aren’t defined by the programmer. When the Promise constructor runs, JavaScript will pass its own resolve() and reject() functions into the executor function.

    • resolve is a function with one argument. Under the hood, if invoked, resolve() will change the promise’s status from pending to fulfilled, and the promise’s resolved value will be set to the argument passed into resolve().
    • reject is a function that takes a reason or error as an argument. Under the hood, if invoked, reject() will change the promise’s status from pending to rejected, and the promise’s rejection reason will be set to the argument passed into reject().

    Let’s look at an example executor function in a Promise constructor:

     

    const executorFunction = (resolve, reject) => {
      if (someCondition) {
          resolve('I resolved!');
      } else {
          reject('I rejected!'); 
      }
    }
    const myFirstPromise = new Promise(executorFunction);

     

     

    Let’s break down what’s happening above:

    • We declare a variable myFirstPromise
    • myFirstPromise is constructed using new Promise() which is the Promise constructor method.
    • executorFunction() is passed to the constructor and has two functions as parameters: resolve and reject.
    • If someCondition evaluates to true, we invoke resolve() with the string 'I resolved!'
    • If not, we invoke reject() with the string 'I rejected!'

    In our example, myFirstPromise resolves or rejects based on a simple condition, but, in practice, promises settle based on the results of asynchronous operations. For example, a database request may fulfill with the data from a query or reject with an error thrown. In this exercise, we’ll construct promises which resolve synchronously to more easily understand how they work.

     

    const inventory = {
      sunglasses: 0,
      pants: 1088,
      bags: 1344
    };
    
    // Write your code below:
    const myExecutor = (resolve, reject) => {
        if (inventory.sunglasses > 0) {
            resolve('Sunglasses order processed.');
        } else {
            reject('That item is sold out.');
        }
    };
    
    const orderSunglasses = () => {
        return new Promise(myExecutor);
    };
    
    const orderPromise = orderSunglasses();
    
    console.log(orderPromise); 

     

     

    출처: https://www.codecademy.com/courses/introduction-to-javascript/lessons/promises/exercises/constructing-promises

     

    | Codecademy

    Codecademy is the easiest way to learn how to code. It's interactive, fun, and you can do it with your friends.

    www.codecademy.com

     

    'Study > JavaScript' 카테고리의 다른 글

    Consuming Promises  (0) 2020.05.15
    The Node setTimeout() Function  (0) 2020.05.15
    What is a Promise?  (0) 2020.05.15
    Introduction(promise)  (0) 2020.05.15
    Combining Export Statements  (0) 2020.05.14

    댓글

Designed by Tistory.