My answer is using the usual Node.js promises (this is probably easy to adapt to Bluebird or another library).
You can immediately disable all promises using Promise.all :
var groupsArray = [ {'G1': ['C1','C2','C3']}, {'G2': ['D1','D2','D3']} ]; function ajax(url) { return new Promise(function(resolve, reject) { request.get(url,{json: true}, function(error, data) { if (error) { reject(error); } else { resolve(data); } }); }); } Promise.all(groupsArray.map(group => ajax("your-url-here"))) .then(results => {
Using Promise.all tries to fulfill all your requests in parallel. This probably won't work when you have 500 million requests so that all attempts are made at the same time!
A more efficient way to do this is to use the JavaScript reduce function to consistently match your requests one by one:
// ... Setup as before ... const results = []; groupsArray.reduce((prevPromise, group) => { return prevPromise.then(() => { return ajax("your-url-here") .then(result => { // Process a single result if necessary. results.push(result); // Collect your results. }); }); }, Promise.resolve() // Seed promise. ); .then(() => { // Code that depends on all results. }) .catch(err => { // Handle the error. });
This example combines promises, so that the next only starts after the previous is complete.
Unfortunately, the sequencing process will be very slow, because it has to wait for the completion of each request before starting a new one. Although each request is executed (it takes time for the API request), your processor is idle, while it can work with another request!
A more effective but complex approach to this problem is to use a combination of the above approaches. You must fulfill your requests so that the requests in each batch (say 10) are executed in parallel, and then the parties are sequentially ordered one after another.
It is difficult to implement this on your own - although this is a great training exercise - using a combination of Promise.all and reduce , but I would suggest using the async-await-parallel library. There are a bunch of such libraries, but I use this one and it works well and it does the right job easily.
You can install the library as follows:
npm install --save async-await-parallel
Here's how you use it:
const parallel = require("async-await-parallel"); // ... Setup as before ... const batchSize = 10; parallel(groupsArray.map(group => { return () => { // We need to return a 'thunk' function, so that the jobs can be started when they are need, rather than all at once. return ajax("your-url-here"); } }, batchSize) .then(() => { // Code that depends on all results. }) .catch(err => { // Handle the error. });
It's better, but it's still a clumsy way to make such a large number of requests! You may need to raise your bid and consider investing in proper asynchronous job management.
I used Kue recently to manage a workflow cluster. Using Kue with the Node.js cluster library allows you to get the proper parallelism happening on a multi-core PC, and then you can easily expand it to combine cloud-based virtual machines if you need to grumble even more.
See my answer here for Kue example code.