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
}
via torbenrudgaard
No comments:
Post a Comment