All Projects → JMPerez → Promise Throttle

JMPerez / Promise Throttle

Licence: mit
A small library to throttle promises. Useful to avoid rate limiting when using REST APIs.

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Promise Throttle

Ws
⚠️ Deprecated - (in favour of Networking) ☁️ Elegantly connect to a JSON api. (Alamofire + Promises + JSON Parsing)
Stars: ✭ 352 (+190.91%)
Mutual labels:  rest-api, promise
Poloniex Api Node
Poloniex API client for REST and WebSocket API
Stars: ✭ 138 (+14.05%)
Mutual labels:  rest-api, promise
Express Rest Boilerplate
⌛️ Express starter for building RESTful APIs
Stars: ✭ 1,794 (+1382.64%)
Mutual labels:  rest-api
Elassandra
Elassandra = Elasticsearch + Apache Cassandra
Stars: ✭ 1,610 (+1230.58%)
Mutual labels:  rest-api
Zousan
A Lightning Fast, Yet Very Small Promise A+ Compliant Implementation
Stars: ✭ 117 (-3.31%)
Mutual labels:  promise
Dinoloop
Rest API framework built on top of expressjs powered by Typescript.
Stars: ✭ 115 (-4.96%)
Mutual labels:  rest-api
Bach
Compose your async functions with elegance.
Stars: ✭ 117 (-3.31%)
Mutual labels:  promise
Automatic Api
A list of software that turns your database into a REST/GraphQL API
Stars: ✭ 1,583 (+1208.26%)
Mutual labels:  rest-api
Lips
Scheme based powerful lisp interpreter written in JavaScript
Stars: ✭ 120 (-0.83%)
Mutual labels:  promise
Coronavirus Tracker Api
🦠 A simple and fast (< 200ms) API for tracking the global coronavirus (COVID-19, SARS-CoV-2) outbreak. It's written in python using the 🔥 FastAPI framework. Supports multiple sources!
Stars: ✭ 1,577 (+1203.31%)
Mutual labels:  rest-api
Ipokemon Server
iPokeMon Server.
Stars: ✭ 119 (-1.65%)
Mutual labels:  rest-api
Spring Webflux Reactive Rest Api Demo
Build Reactive Rest APIs with Spring WebFlux and Reactive Mongo
Stars: ✭ 117 (-3.31%)
Mutual labels:  rest-api
Article
前端相关、CSS、JavaScript、工具、解决方案…相关文章
Stars: ✭ 116 (-4.13%)
Mutual labels:  promise
Rest Api Fuzz Testing
REST API Fuzz Testing (RAFT): Source code for self-hosted service developed for Azure, including the API, orchestration engine, and default set of security tools (including MSR's RESTler), that enables developers to embed security tooling into their CI/CD workflows
Stars: ✭ 119 (-1.65%)
Mutual labels:  rest-api
Postmate
📭 A powerful, simple, promise-based postMessage library.
Stars: ✭ 1,638 (+1253.72%)
Mutual labels:  promise
Stop.js
🐦 The Promise base `setTimeout`, release your callback
Stars: ✭ 120 (-0.83%)
Mutual labels:  promise
Elemental2
Type checked access to browser APIs for Java code.
Stars: ✭ 115 (-4.96%)
Mutual labels:  promise
Laravel Hateoas
Expose the authorization logic of your REST API using HATEOAS links
Stars: ✭ 116 (-4.13%)
Mutual labels:  rest-api
Drf Api Tracking
Fork of aschn/drf-tracking so that we can maintain and release newer versions
Stars: ✭ 117 (-3.31%)
Mutual labels:  rest-api
Diskover Web
Web file manager, disk space usage, storage search engine and file system analytics for diskover
Stars: ✭ 121 (+0%)
Mutual labels:  rest-api

Promise Throttle   Build Status Coverage Status Greenkeeper badge

This small (~530B minified and compressed) dependency-free library limits promises run per unit of time. Useful for Rest API consumption, which is normally rate-limited to a certain number of requests in a set amount of time.

On Node.js, pass the Promise library you are using to the constructor.

To use, simply add functions to the PromiseThrottle that, once called, return a Promise.

Use

The library can be used either server-side or in the browser.

  var PromiseThrottle = require('promise-throttle');
  /**
   * A function that once called returns a promise
   * @return Promise
   */
  var myFunction = function(i) {
    return new Promise(function(resolve, reject) {
      // here we simulate that the promise runs some code
      // asynchronously
      setTimeout(function() {
        console.log(i + ": " + Math.random());
        resolve(i);
      }, 10);
    });
  };

  var promiseThrottle = new PromiseThrottle({
    requestsPerSecond: 1,           // up to 1 request per second
    promiseImplementation: Promise  // the Promise library you are using
  });

  var amountOfPromises = 10;
  while (amountOfPromises-- > 0) {
    promiseThrottle.add(myFunction.bind(this, amountOfPromises))
      .then(function(i) {
        console.log("Promise " + i + " done");
      });
  }

  // example using Promise.all
  var one = promiseThrottle.add(myFunction.bind(this, 1));
  var two = promiseThrottle.add(myFunction.bind(this, 2));
  var three = promiseThrottle.add(myFunction.bind(this, 3));

  Promise.all([one, two, three])
    .then(function(r) {
        console.log("Promises " + r.join(", ") + " done");
    });

Options

weight

You can specify weight option for each promise to dynamically adjust throttling depending on action "heaviness". For example, action with weight = 2 will be throttled as two regular actions. By default weight of all actions is 1.

  var regularAction = promiseThrottle.add(performRegularCall);
  var heavyAction = promiseThrottle.add(performHeavyCall, {weight: 2});

signal

You can cancel queued promises using an AbortSignal. For this, pass a signal option obtained from an AbortController. Once it is aborted, the promises queued using the signal will be rejected.

If the environment where you are running the code doesn't support AbortController, you can use a polyfill.

  var controller = new AbortController();
  var signal = controller.signal;
  var pt = createPromiseThrottle(10);
  pt.addAll([
    function() {
      return fetch('example.com/a');
    },
    function() {
      return fetch('example.com/b');
    },
    function() {
      ...
    }
  ], {signal: signal});
  ...

  // let's abort the promises
  controller.abort();

You can decide to make only specific promises abortable:

  var controller = new AbortController();
  var signal = controller.signal;
  var pt = createPromiseThrottle(10);
  pt.add(function() { return fetch('example.com/a') });
  pt.add(function() { return fetch('example.com/b') }, {signal: signal});
  pt.add(function() { return fetch('example.com/c') });
  ...

  // let's abort the second one
  controller.abort();

When aborting, the promise returned by add or addAll is rejected with a specific error:

  var controller = new AbortController();
  var signal = controller.signal;
  var pt = createPromiseThrottle(10);
  pt.addAll([
    function() {
      return fetch('example.com/a');
    },
    function() {
      return fetch('example.com/b');
    },
    function() {
      ...
    }
  ], {signal: signal}).catch(function(e) {
    if (e.name === 'AbortError') {
      console.log('Promises aborted');
    }
  });
  ...

  // let's abort the promises
  controller.abort();

Installation

For node.js, install the module with: npm i promise-throttle

If you are using it in a browser, you can use bower: bower install promise-throttle

Development

Install the dependencies using npm install. Run npm start to lint, test and browserify promise-thottle.

Projects using it

See how some projects are using it:

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].