mindelay
Pronounced as one word. Sets a minimum delay before a callback can be called, no matter how long it takes for the caller to call it.
Spec
Install
On command line:
$ npm install --save mindelay
In NodeJS:
let mindelay = ;
Alternatively, in browser JavaScript:
Usage
We're using the usecase of an API response, which illustrates the utility of mindelay
best
and is how we use mindelay
in production.
Instead of something like this, which responds as soon as the API responds
;
or something like this, which adds a fixed delay of one second to the API response time, no matter how long the API response time
;
use something like this, which delays by at least one second, but if the API takes a long while it will respond as soon as possible.
;
Cancellation
var wrappedCallback = );
If you ever need to reference the callback directly, use wrappedCallback.call
:
wrappedCallback //will have no delay
If you want to cancel any delay on a wrapped callback, use wrappedCallback.cancel
:
wrappedCallback //will have no delay
Example
Pictured is a chatbot (SkillFlow) that needs to make a query to a Natural Language Processing API before it's able to respond.
We want to add a somewhat natural delay, but must keep in mind that the API may take any amount of time to respond, and
we can't just use setTimeout
and keep the user waiting for extra long.
The solution is of course to use mindelay
instead of setTimeout
.
Copyright ©2016 SkillFlow. MIT License. Created by Clive Chan, with contributions from David Tesler.