npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

batch-request-js

v2.0.2

Published

Batch promise based requests to overcome network limitations

Downloads

653

Readme

batch-request-js CircleCI

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