Sequential execution of Promise

This question already has an answer here:

  • Aren't promises just callbacks? 7 answers

  • You can sequence promises without the ever increasing nesting like this:

    app.post('/Billing', function(req, res) {
        cart.getBasket(req).then(function(basket) {
            return cart.updateBasket(req);
        }).then(function() {
            return cart.updateDefaultShipment(req);
        }).then(function(basket) {
            return cart.getBasketObject(basket);
        }).then(function(basketObj) {
            res.render('billing', {basket: basketObj});
        }).catch(function(error) {
            console.log(error);
        });
    });
    

    Returning a promise from a .then() autoamtically chains it to the parent promise which allows you to use a .then() handler on the parent promise rather than using deeper nesting to continue the sequence.

    This automatically passed the results of the prior promise down the chain to the next operation, but it doesn't pass all prior results to subsequent promises. If you need other prior results, you can see various ways to do that here: How to chain and share prior results with Promises

    链接地址: http://www.djcxy.com/p/55446.html

    上一篇: 如何获得成功实现一系列承诺的第一个承诺的价值

    下一篇: Promise的顺序执行