FAQ: JavaScript Promises - Constructing a Promise Object


#1

This community-built FAQ covers the “Constructing a Promise Object” exercise from the lesson “JavaScript Promises”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Asynchronous JavaScript
Asynchronous JavaScript

FAQs on the exercise Constructing a Promise Object

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!


#2

1.

You’ll be writing your code in the code-editor, but we won’t be running it until the final step. To check your code for a step, you can press the “Check Work” button.

We’re going to create a promise representing ordering sunglasses from an online store. First, create the function, myExecutor() . Later on, you’ll pass this function into the Promise constructor.

myExecutor() should:

  • Have two parameters: resolve and reject
  • Check if the sunglasses property on the inventory object has a value greater than zero
  • If it does, myExecutor() should invoke resolve() with the string 'Sunglasses order processed.'
  • If it does not, myExecutor() should invoke reject() with the string 'That item is sold out.'

The answer to part one above is as follows , but before we dive into the code below , i wanted to highlight the key aspects, that we are using the ES6 syntax , arrow functions, the 'new ’ keyword
for objects and the use of 'const ’ to declare variables .Ok now , the fun part .

const myExecutor = (resolve, reject) => {
    if (inventory.sunglasses > 0) {
        resolve('Sunglasses order processed.');
    } else {
        reject('That item is sold out.');
    }
};

The explanation is very simple , we are creating a arrow function and then we are making use of if and else statements in to check for a certain condition in the object inventory and we do that by referencing the object and its properties inside like below , this is very important because we can not just reference the property directly of an object .JS doesn’t know that

if (inventory.sunglasses > 0) {
        resolve('Sunglasses order processed.');

2.

Create a function, orderSunglasses() . This function should have no parameters. It should return a new promise constructed by passing your myExecutor() function into the Promise constructor.

const orderSunglasses = () => {
    return new Promise(myExecutor);
};

```This is pretty self explanatory 

**3.**

Create a variable  `orderPromise`  assigned to the returned value of your  `orderSunglasses()` function.

const orderPromise = orderSunglasses();


This is even easier , hope this helps the students .

#3

Why is the function orderSunglasses used, as opposed to simply creating a variable which is set equal to a new promise.

let order = new Promise(myExecutor);
console.log(order);