Last active
July 29, 2019 20:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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