This question already has an answer here:
- Aren't promises just callbacks? 8 answers
I have following promise functions implemented like
return new Promise(function(resolve, reject) { //logic });
- cart.getBasket(req)
- cart.updateBasket(req)
- cart.updateDefaultShipment(req)
- cart.getBasketObject(basket)
Currently I execute code using
app.post('/Billing', function(req, res) {
cart.getBasket(req).then(function(basket) {
cart.updateBasket(req).then(function() {
cart.updateDefaultShipment(req).then(function(basket) {
cart.getBasketObject(basket).then(function(basketObj) {
res.render('billing', {
basket: basketObj
});
});
});
});
}).catch(function(error) {
console.log(error);
});
});
I read about Promise.each
and thought of using it as the flow of my logic has to be sequential but when I use Promise.each
it doesn't work as expected because I saw that inner function of each
returns value of every promise execution.
app.post('/Billing', function(req, res) {
var asyncCalls = [cart.getBasket(req), cart.updateBasket(req), cart.updateDefaultShipment(req), cart.getBasketObject(basket)];
Promise.each(asyncCalls, function(basketObj){
res.render('billing', {
basket: basketObj
});
});
});
So, is there any cleaner way available to achieve what am doing with .then
chain i.e have a cleaner chain.
Also, is it possible in sequential promise execution that the next promise function gets the return value of the previous promise which got executed.
PS : Promise array length will know beforehand.