jamesdlivesinatree
jamesdlivesinatree

Reputation: 720

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.

Upvotes: 0

Views: 30

Answers (1)

Kashif
Kashif

Reputation: 1263

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

Upvotes: 1

Related Questions