Chaining Requests using BlueBird/ Request-Promise

◇◆丶佛笑我妖孽 提交于 2019-12-07 16:12:30

Here you go:

var BPromise = require('bluebird');
var rp = require('request-promise');

BPromise.all([
    rp(optionsForRequest1),
    rp(optionsForRequest2)
])
    .spread(function (responseRequest1, responseRequest2) {
        // Proceed with other calls...
    })
    .catch(function (err) {
        // Will be called if at least one request fails.
    });

Like you said, you can accomplish this using the all API.

Refer to the documentation here: http://bluebirdjs.com/docs/api/promise.all.html

Example:

    var self = this;
    return new Promise(function(resolve) {
        Promise.all([self.createUser, self.createAddress])done(
            // code path when all promises are completed
            // OR should any 1 promise return with reject()
            function() { resolve(); }
        );
    })

As noted in the code, the .all() callback code path will get called as well when any 1 of the defined promise in promises gets rejected.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!