The gate-keeper!
If you have some remote resource you want to fetch, and you could be requesting a bunch of times, you probably don't need more than one request going at a time.
This library uses Promises. For the callback version, see the 1.x branch.
Pairs very well with the key-master.
Install
npm install gate-keeper
const gateKeeper = require('gate-keeper')
Example
let calledAlready = falseconst get = get // => true
API
const get = gateKeeper(asyncGetterFunction)
Returns a get
function that you can use whenever you want to trigger calling asyncGetterFunction
.
asyncGetterFunction
should return a promise. Until that promise is resolved or rejected, asyncGetterFunction
will not be called again.
Your asyncGetterFunction
function will be passed an object with a property named isCancelled
, a function you can call that returns true
or false
depending on whether or not the gatekeeper's cancel
method was called while the request was running.
get()
Triggers the asyncGetterFunction
if it is not running already. Returns the promise associated with the currently-running getter.
get.isCurrentlyGetting()
Returns true
if the asyncGetterFunction
is in progress, false
otherwise.
get.cancel()
If the asyncGetterFunction
is in progress, its results are ignored. Promises that are unresolved when cancel
is called will never resolve.
Using with the key-master
Because you probably have more than one remote resource you could be fetching.
const keyMaster = const fetchers = fetchers { value // => 'pie is awesome!'}fetchers { value // => 'cake is awesome!'}fetchers { value // => 'pie is awesome!'}