Batch promise based requests to overcome network limitations or API restrictions
Install
yarn add batch-request-js
Tests
yarn test-unit
to run unit testsyarn test-e2e
to run e2e testsyarn test-ci
to run both unit and e2e tests
Usage
Suppose we'd like to fetch thousands of customers from an API. To avoid network limitations or rate limiting issues, we can batch the requests:
// node.jsconst batchRequest = { // define array of input data e.g. customerIds const customerIds = '100' '101' '102' ... // define the async request to perform against each data input const request = // fetch customers in batches of 100, delaying 200ms inbetween each batch request const error data = await // Data from successful requests console // [{ customerId: '100', ... }, ...] // Failed requests console // [{ record: "101", error: [Error: Customer not found] }, ...]}
Example
// node.jsconst batchRequest = { // setup a 100 test records const records = Array100 // all requests will succeed and be timestamped const request = Promise // batch requests 20 at a time, delaying half a second after each batch request const result = await console// { // error: [],// data: [// { record: 0, timestamp: 1533552890663 },// { record: 1, timestamp: 1533552890663 },// { record: 2, timestamp: 1533552890663 },// { record: 3, timestamp: 1533552890663 },// ...// ]}
Handling failed batch requests
Rerun batch request with a filtered set of inputRecords
to just those that failed on the previous attempt.
Future
Retry logic may be implemented to handle automatically rerunning batch-request for failing requests.
License
MIT