FAQ: JavaScript Promises - Constructing a Promise Object

I would like to understand why we don’t invoke the myExecutor function here:
function orderSunglasses() { return new Promise(myExecutor); };

and so why this is actually running and returning a value :
function myExecutor(resolve, reject){ if (inventory.sunglasses > 0) { resolve('Sunglasses order processed.'); } else { reject('That item is sold out.'); } };

thanks for your help

Because we want the Promise to invoke it. It is passed as a reference only the same way we would pass a callback.

1 Like

Why is this a = b = c, not just a=c? What purpose do the function and variable perform?

console.log(new Promise(myExecutor));

Works exactly the same, so why bother with the extra steps???

The way you type still works. The exercise just probably tried to show how arguments were passed down from myExecutor to new Promise(), I guess?

4 posts were split to a new topic: Why do we use Bash all of a sudden?

asking myself the same!

it was recommended in a previous lesson to avoid using the Arrow function, as in some specific cases (that I forgot) it can create error.

but in most of the following exercices, we are forced to use the arrow function.
I don’t understand.

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

From example:

const myFirstPromise = new Promise(executorFunction);

Is this wrong?, I followed this example but didn’t work… step 2.

13 months later and still the same. Thanks for the insights.

mine is not working too

What the ■■■■? That fixed my problem. There’s nothing wrong with the code at all.

I understand your frustration as I’ve experienced the same thing. However I think when you want a future in programming you have to be very precise in the work you do. Being strict in these assignments actually helps us develope this skill!

1 Like

What is the point of the bash terminal?

So I skipped the step 3 to create another variable with the value of the Promise function and just log my promise function variable as it was in the example
const myExecutor = (resolve, reject) => {

if(inventory.sunglasses  > 0) {

    resolve('Sunglasses order processed.');

  } else {

    reject('That item is sold out.');



const orderSunglasses = new Promise(myExecutor);


This is working also fine, so what the purpose of created another variable ? I understood in one the first lessons that the less we have variable in the global scope, cleaner is our code and here it’s the opposite we are requested… :face_with_monocle:

There’s actually two reasons for that:

  1. this usually (will explain later) scopes to the parent object of the module (function inside an object) in which it’s written. For example, if you had written the function as a module of inventory like so:
const inventory = {
  sunglasses: 1,
  pants: 1088,
  bags: 1344,
  myExecutor(resolve, reject) {
    if(this.sunglasses > 0) {
      resolve('Sunglasses order processed.');
    } else {
      reject('That item is sold out.');

the this.sunglasses, would reference as intended by you, because this refers to inventory, the parent of the module (function) myExecutor.
Important: notice that I did NOT use an arrow function in declaring my module, as that would not work. I COULD have used a normal function declaration though function myExecutor () {}; , and that would have worked just aswell.

Which brings us to point two:

  1. this does not work the same way inside arrow functions.

In an arrow function the keyword this isn’t bound to it’s functions parent (object in which function is inserted), and I recomend reading documentation on it.

Disclaimer: don’t trust my every word as absolute true, as I’m also still learning.