承诺会给您
Promise.all()(对于本地承诺以及诸如bluebird的库承诺都是如此)。
更新 :从节点8开始,您可以
util.promisify()像使用Bluebird一样使用
.promisify()
var requestAsync = util.promisify(request); // const util = require('util')var urls = ['url1', 'url2'];Promise.all(urls.map(requestAsync)).then(allData => { // All data available here in the order of the elements in the array});因此,您可以做什么(本机):
function requestAsync(url) { return new Promise(function(resolve, reject) { request(url, function(err, res, body) { if (err) { return reject(err); } return resolve([res, body]); }); });}Promise.all([requestAsync('url1'), requestAsync('url2')]) .then(function(allData) { // All data available here in the order it was called. });如果您有蓝鸟,这甚至更简单:
var requestAsync = Promise.promisify(request);var urls = ['url1', 'url2'];Promise.all(urls.map(requestAsync)).then(allData => { // All data available here in the order of the elements in the array});


