pacedcaller
TypeScript icon, indicating that this package has built-in type declarations

0.2.1 • Public • Published

Paced Caller

What is this?

Library designed to allow async calls to a function to be made over time in a controlled manner.

ie -> I want to call a function for each element in a loop, each call should be spaced 2000 milliseconds apart.

Why would I want to do this?

Say you want to hit an API with a rate limit, and need requests to be spaced X time apart.

Say you want to loop through an array, and execute a function, but each iteration must only occur after the last iteration.

How can I use this?

Import the library with

npm install pacedcaller

The library is designed with typescript in mind so:

import {pacedCall} from 'pacedcaller'

Declare a paced called:

let calls = new pacedCall(iterationData, timeDelay, actions, fragmentationSize);`

iterationData - Array of any type data, of any size.

timeDelay - Time in milliseconds between each loop.

actions - Function to repeat on each element, takes in an ARRAY of elements (fragment) and returns an array of results.

fragmentationSize - The size of a fragment.

Woah Hey.. What is a fragment?

The library is designed to 'batch' runs. For instance imagine hitting an api that accepts 200 locations at a time in an array. But you have an array of 16k sites. This function will 'batch' the 16k array into 'blocks' of 200. For this reason you 'action' function accepts an ARRAY of data, not a single point of data.

It's perfectly valid to have a single point of data in an array though.

For example, You have an set of lat/lng points [{lat:x,lng:y}, {lat:x,lng:y} ...etc x 16,000]

You have an API that accepts 200 points at a time.

Simply instantiate a pacedCall class:

const latlngArray = [...{'lat':x,'lng':y}]
let calls = new pacedCall(
  [...latLngArray],
  2000,
  (latlngSubset) => {
    makeAPICall(latlngSubset);
  },
  200
);

The result will be like:

[
  { request: { lat: x, lng: x }, result: result },
  { request: { lat: x, lng: x }, result: result },
  { request: { lat: x, lng: x }, result: result },
  { request: { lat: x, lng: x }, result: result }... x 16,000
];

TODO:

  • Allow actions to take in a single value not in an array.
  • Allow an option to execute each loop directly after the previous loop finishes, as opposed to requiring a time between each iteration.

Dependencies (2)

Dev Dependencies (3)

Package Sidebar

Install

npm i pacedcaller

Weekly Downloads

11

Version

0.2.1

License

MIT

Unpacked Size

23.5 kB

Total Files

11

Last publish

Collaborators

  • manojclsdkumar