GithubHelp home page GithubHelp logo

kunal-mandalia / batch-request-js Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 1.0 64 KB

Batch promise based requests to overcome network / API restrictions

Home Page: https://www.npmjs.com/package/batch-request-js

License: MIT License

JavaScript 100.00%
batch javascript nodejs

batch-request-js's Introduction

batch-request-js CircleCI Coverage Status

Batch promise based requests to overcome network limitations or API restrictions

Install

  • yarn add batch-request-js

Tests

  • yarn test-unit to run unit tests
  • yarn test-e2e to run e2e tests
  • yarn 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.js
const batchRequest = require('batch-request-js')


async function getCustomers () {
  // 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 = (customerId) => fetch(`${API_ENDPOINT}/${customerId}`).then(response => response.json())

  // fetch customers in batches of 100, delaying 200ms inbetween each batch request
  const { error, data } = await batchRequest(customerIds, request, { batchSize: 100, delay: 200 })

  // Data from successful requests
  console.log(data) // [{ customerId: '100', ... }, ...]

  // Failed requests
  console.log(error) // [{ record: "101", error: [Error: Customer not found] }, ...]
}

Example

// node.js
const batchRequest = require('batch-request-js')

async function getData () {
  // setup a 100 test records
  const records = Array(100).fill(0).map((d, i) => ({ item: i }))
  // all requests will succeed and be timestamped
  const request = record => Promise.resolve({ ...record, timestamp: Date.now() })
  // batch requests 20 at a time, delaying half a second after each batch request
  const result = await batchRequest(records, request, { batchSize: 20, delay: 500 })
  console.log(result)
//   { 
//     error: [],
//     data: [
//        { record: 0, timestamp: 1533552890663 },
//        { record: 1, timestamp: 1533552890663 },
//        { record: 2, timestamp: 1533552890663 },
//        { record: 3, timestamp: 1533552890663 },
//         ...
//     ]
}

getData()

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.