0.2.1 • Published 5 years ago

pacedcaller v0.2.1

Weekly downloads
1
License
MIT
Repository
-
Last release
5 years ago

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.
0.2.1

5 years ago

0.2.0

5 years ago

0.1.7

5 years ago

0.1.5

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago

0.0.3

5 years ago

0.0.2

5 years ago

0.0.1

5 years ago