最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use Promise.all with Request in node.js? - Stack Overflow

programmeradmin0浏览0评论

I'd like to do something along the lines of:

Promise.all([
    fetch(url1).then(function(response){ return response.json() }),
    fetch(url2).then(function(response){ return response.json() }),
    fetch(url3).then(function(response){ return response.json() }),
    fetch(url4).then(function(response){ return response.json() })
]).then(allResponses => {    
    var data1 = allResponses[0];
    var data2 = allResponses[1];
    var data3 = allResponses[2];
    var data4 = allResponses[3];

    // process data....
});

The above is React.js code, I'd like to do the same thing but with Node.js on the server. Problem is I don't have fetch, I have request (should I even be using Request?). It's used in this way...

var request = require('request');

request(url1, function (error, response, body) {
});

But how would I use request with Promise.all? Because I want to fetch multiple things and only process them when all are done but in Node, and I'd rather not use a bunch of callbacks.

I'd like to do something along the lines of:

Promise.all([
    fetch(url1).then(function(response){ return response.json() }),
    fetch(url2).then(function(response){ return response.json() }),
    fetch(url3).then(function(response){ return response.json() }),
    fetch(url4).then(function(response){ return response.json() })
]).then(allResponses => {    
    var data1 = allResponses[0];
    var data2 = allResponses[1];
    var data3 = allResponses[2];
    var data4 = allResponses[3];

    // process data....
});

The above is React.js code, I'd like to do the same thing but with Node.js on the server. Problem is I don't have fetch, I have request (should I even be using Request?). https://github./request/request It's used in this way...

var request = require('request');

request(url1, function (error, response, body) {
});

But how would I use request with Promise.all? Because I want to fetch multiple things and only process them when all are done but in Node, and I'd rather not use a bunch of callbacks.

Share Improve this question edited Mar 29, 2024 at 10:20 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Mar 30, 2018 at 19:22 Shai UIShai UI 51.9k77 gold badges217 silver badges316 bronze badges 2
  • 2 you can always install fetch on node too. – dfsq Commented Mar 30, 2018 at 19:25
  • 1 Have you tried looking into request-promise? It makes it so requests are promises. npm install request-promise then add var request = require('request-promise'); – BShaps Commented Mar 30, 2018 at 19:27
Add a ment  | 

3 Answers 3

Reset to default 4

More concisely, after installing node-fetch, you can make a promise-returning fetch that resolves to json...

const fetch = require('node-fetch');
function fetchJSON(url) {
    return fetch(url).then(response => response.json());
}

...build an array of promises from an array of urls...

let urls = [url1, url2, url3, url4];
let promises = urls.map(url => fetchJSON(url));

...do something when those promises resolve...

Promise.all(promises).then(responses => console.log(responses));

Convert the requests into promises:

function promiseRequest(url) {
  return new Promise(resolve => {
    request(url, function(err, response, body) {
      resolve(body);
    });
  });
}

Then you can do something like

const allBodies = await Promise.all([
  promiseRequest(url1),
  promiseRequest(url2),
  promiseRequest(url3)
]);

But it would be nicer to use fetch natively.

I just did

npm install node-fetch

and then

var fetch = require('node-fetch');

and then I could use my code above:

Promise.all([
    fetch(url1).then(function(response){ return response.json() }),
    fetch(url2).then(function(response){ return response.json() }),
    fetch(url3).then(function(response){ return response.json() }),
    fetch(url4).then(function(response){ return response.json() })
]).then(allResponses => {
    var data1 = allResponses[0];
    var data2 = allResponses[1];
    var data3 = allResponses[2];
    var data4 = allResponses[3];

    // process data....
});
发布评论

评论列表(0)

  1. 暂无评论