Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

timseverien/throttled-promise

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ThrottledPromise

Promises are great, but the API only lets us run things in parallel or sequence. But sometimes, you don’t want to run all processes in parallel. To keep CPU/network/memory usage to a minimum for example. In that case, you may want to throttle the amount of parallel processes. This is what ThrottledPromise is for.

Installation

npm install throttled-promise

or

bower install throttled-promise

Usage

const ThrottledPromise = require('throttled-promise');
const promises = [
    new ThrottledPromise((resolve, reject) => ...),
    new ThrottledPromise((resolve, reject) => ...),
    new ThrottledPromise((resolve, reject) => ...)
];

// Run promises, but only 2 parallel
ThrottledPromise.all(promises, 2)
.then(...)
.catch(...);

ThrottlePromise is not a substitution for Promise. It does not have the .then() and .catch() methods. It’s only meant to use for ThrottledPromise.all(). However, ThrottledPromise.all() does return a Promise object and can be chained as seen in the example above.

Note

Some environments, older browsers for example, do not support Promises. To use this module on those environments, you will need a polyfill. I highly recommend Jake Archibald’s es6-promise, but any polyfill that follows the specification should do.

For node.js, you may want to use the --harmony flag instead of a polyfill to get support for Promises.

About

Similar to Promise.all, throttled-promise runs parallel promises, but allows you to throttle them.

Resources

License

Stars

Watchers

Forks

Packages

No packages published