jamesdlivesinatree jamesdlivesinatree - 1 year ago 50
Javascript Question

Constructing proper promises in node using a+

I have a function:

calculateDemand(skills) {
let average = 0;
let demandArray = [];
// logic to make demandArray equal something
Promise.resolve(demandArray);
}


which I call inside another function here

this.calculateDemand(skills).then((demandArray) => {
console.log(demandArray);
})


I get an error saying I am calling then on undefined. I have been reading documentation on promisejs.org on A+ implementation of Promises, but I am not sure how to solve this simple use case. Could someone provide an example of how I would correct this code?

Before this, I was constructing new promises everytime using the
constructor anti-pattern
, so I am looking for best-practice.

Answer Source

You have to change your last line to read return Promise.resolve(demandArray); in calculateDemand()