Nodejs使用Bluebird响应请求并行请求

时间:2017-12-16 15:51:43

标签: node.js request bluebird cheerio request-promise

我有两个网址,我一直在请求并使用Bluebird promise library想要用cheerio处理网址的html。我似乎无法获得结果html。我应该在传播内使用什么?

    let url1 = request('http://example1.com')
    let url2 = request('http://example2.com')

    Promise.all([url1, url2])
    .spread(function (url1RqRes, url2RqRes) {

        // How do I get access to the response html here ???

    })
    .catch(function (err) {
        console.log(err)
    });

1 个答案:

答案 0 :(得分:0)

var cheerio = require('cheerio'); // Basically jQuery for node.js
var rp = require('request-promise');

let url1 = rp('http://example1.com')
let url2 = rp('http://example2.com')

Promise.all([url1, url2])
.then(function (results) {
    console.log('results', results)
    let pages = results.map((resp) => cheerio.load(resp))
    // you will get the result in the same order of promise passed as array
})
.catch(function (err) {
    console.log(err)
});

供参考request-promise with cheerio

相关问题