jamesdlivesinatree jamesdlivesinatree - 3 years ago 85
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

which I call inside another function here

this.calculateDemand(skills).then((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()

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download