torbenrudgaard torbenrudgaard - 5 months ago 23
Javascript Question

Run 3 promises, then run the last promise once the others finish

Im writing a nodejs controller.

I have a whole set of promises where each of them get data from different queries

I want to start them all (because they take different time to complete) and once they are all finished, THEN I want to start my priceCalc function which uses all the data collected.

I looked at a lot of guides and examples, but I dont understand the promise.all and how it could work in my case?

Also, what if one of them fails, it should return an error? They must all complete for the priceCalc function to start.

(im a newbie to promises, so please make it simple if you can :-)))

var findPrice = new Promise(
(resolve, reject) => {
priceTable.findOne........

var movePrice = function (data) {
priceArray = data......

findPrice
.then(movePrice)
.then(fulfilled => console.log('findPrice FINISHED!!!'))
.catch(error => console.log(error.message));


var findhotdeal = new Promise(
(resolve, reject) => {
hotdealTable.aggregate.........

var movehotdeal = function (data) {
hotdealArray = data;.......

findhotdeal
.then(movehotdeal)
.then(fulfilled => console.log('findhotdeal FINISHED!!!'))
.catch(error => console.log(error.message));


var calculatePrice = function () {
// Use the data from the previous promises once they all finished

}

Answer Source

If you need the resolved values of all 4 promises

var findPrice = new Promise(
    (resolve, reject) => {
    priceTable.findOne........

var movePrice = function (data) {
    priceArray = data......

var findhotdeal = new Promise(
    (resolve, reject) => {
    hotdealTable.aggregate.........

var movehotdeal = function (data) {
    hotdealArray = data;.......     

var calculatePrice = function ([fp, mp, fh, mh]) {
    // fp == resolved value of findPrice
    // mp == resolved value of movePrice
    // fh == resolved value of findhotdeal
    // mh == resolved value of movehotdeal
}

Promise.all([findPrice, findPrice.then(movePrice), findhotdeal, findhotdeal.then(movehotdeal)])
    .then(calculatePrice)
    .catch(err => { /* handle errors here */})

Note:

var calculatePrice = function ([fp, mp, fh, mh]) {
    // fp == resolved value of findPrice
    // mp == resolved value of movePrice
    // fh == resolved value of findhotdeal
    // mh == resolved value of movehotdeal
}

is ES2015+ shorthand for

var calculatePrice = function (r) {
    var fp = r[0],
        mp = r[1],
        fh = r[2],
        mh = r[3];

    // fp == resolved value of findPrice
    // mp == resolved value of movePrice
    // fh == resolved value of findhotdeal
    // mh == resolved value of movehotdeal
}

Given that movePrice and movehotdeal are not asyncrhonous

var findPrice = new Promise((resolve, reject) => {
    priceTable.findOne........
});

var findhotdeal = new Promise((resolve, reject) => {
    hotdealTable.aggregate.........
});

var calculatePrice = function ([fp, fh]) {
    priceArray = fp;
    hotdealArray = fh;
    // fp == resolved value of findPrice
    // fh == resolved value of findhotdeal
}

Promise.all([findPrice, findhotdeal])
    .then(calculatePrice)
    .catch(err => { /* handle errors here */})