directly
Like Promise.all, only less so
This module could more descriptively be named Promise.allButNotAllAtOnce. It takes an array of functions, each of which return a promise, and returns a promise which resolves once all those promises have resolved, or otherwise rejects... very similar to Promise.all
. The difference is that a maximum of n
promises are created at any one time. This is useful for rate-limiting asynchronous calls (e.g. fetch
, mongoose
...)
*** New feature ***
Now supports throttling of potentially infinite queues of Promises (see notes on the Queue
class below)
About the name
In the West Country people will often promise to do things 'directly' [drekt-lee]
, meaning they'll do it when they're good and ready, possibly never. Example usage:
I'll wash the dishes directly, my lover
Usage
const directly = ;const urls = ; // a big array of urlsconst fetchers = urls; ;
Can also be called as a constructor (in which case the .run()
method should be used)
const Directly = ;const throttledRequests = 10 fetchers throttledRequests // can be used to stop the directly instance prematurelythrottledRequests
To handle an infinite queue of promises use the Queue
class to wrap your array of functions
fetchers = fetchers; ; // use push to add to the execution queue. Will work even if the queue has fallen idlefetchers
Based on an idea originally developed at the FT