FAQ: JavaScript Promises - Constructing a Promise Object

Getting same error on my side. typing node. app.js is not working.

1 Like

Actually got it fixed. Restart the bash terminal, by clicking the x on top to close and open a new session. works after that.

17 Likes

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);

Perhaps it is to nest multiple promise returns inside one function block, in this case orderSunglasses, in which case it may come in handy for future lessons involving arrays and multiple data types. Or it could be just more typing practice.

Thank you! that worked.

@mtf ? I;m also curious about this

So they don’t have to be in any particular order?

Can the parameters of the executor function have alternative names?

I have tried to rename resolve as ok, reject as fail and everything just works, so I believe naming the parameters as resolve and reject is just conventional.


Ok, it said I passed but I have no idea what this means…

Never mind restart bash terminal. :slight_smile:

thank you for sharing !!!

@limp13, instead of

const myExecutor(resolve, reject) {

you may want to write

const myExecutor = (resolve, reject) =>{

or

function myExecutor(resolve, reject) {

const inventory = {

sunglasses: 1900,

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);

this is my code but when i run ‘node app.js’ in bash , errors come.
can anyone tell me why is this happening?

You have a semicolon between the end of your if statement and else (this is in the middle of a statement, not at the end of one):

...
if (inventory.sunglasses> 0){

resolve('Sunglasses order processed.');
}; <----- Remove this semicolon here

else {
...

Generally, don’t put any semicolons after curly braces. Putting them after round braces, declarations and assignments is a good practice though

1 Like

To all those complaining about needing to use arrow functions, you don’t - in my case the ‘new Promise’ was failing because it needed a ‘return’ statement prepending. It just is massively unhelpful that Codecademy don’t give you the option to specify which you want to use for the ‘view solution’.

I don’t use ECMA 6 as it doesn’t work with Google Tag Manager

I found something interesting in this exercise.

If rather than the prefered solution to check if sunglasses have been ordered

if (inventory.sunglasses > 0){

one instead write

if (inventory.sunglasses){

it will pass.

While both will return false for 0 and true for anything higher than 0, the later will also return true for negative numbers - so should it really pass?