Skip to content

Instantly share code, notes, and snippets.

@JacobKnaack
Last active July 29, 2019 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JacobKnaack/badf33f8472d1c49e82c7f74cbd646c9 to your computer and use it in GitHub Desktop.
Save JacobKnaack/badf33f8472d1c49e82c7f74cbd646c9 to your computer and use it in GitHub Desktop.
Node Module for performing a large number of http requests in a sensible manner.
'use strict';
/**
* Executes a large number of requests using asynchronous operations
* @param {Array} reqArray - Contains request objects formatted as per the specifications of whatever request function you are using
* @param {Function} httpPromise - A function that makes a 1 http request such as npm@request-promise: https://www.npmjs.com/package/request-promise#get-something-from-a-json-rest-api
*/
module.exports = (reqArray, httpPromise) => {
const promiseArray = reqArray.map(req => new Promise(async (resolve, reject) => {
try {
resolve(await httpPromise(req));
} catch (err) {
reject(err);
}
}));
return Promise.all(promiseArray)
.then((responses) => responses)
.catch(err => {
return { "message": "bulk request failed", "error": err }
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment