Package Exports
- delay
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (delay) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
delay 
Delay a promise a specified amount of time
Install
$ npm install delay
Usage
const delay = require('delay');
(async () => {
bar();
await delay(100);
// Executed 100 milliseconds later
baz();
})();
API
delay(milliseconds, [options])
Create a promise which resolves after the specified milliseconds
.
delay.reject(milliseconds, [options])
Create a promise which rejects after the specified milliseconds
.
milliseconds
Type: number
Milliseconds to delay the promise.
options
Type: Object
value
Type: any
Optional value to resolve or reject in the returned promise.
signal
Type: AbortSignal
The returned promise will be rejected with an AbortError if the signal is aborted. AbortSignal is available in all modern browsers and there is a ponyfill for Node.js.
delayPromise.clear()
Clears the delay and settles the promise.
Advanced usage
Passing a value:
const delay = require('delay');
(async() => {
const result = await delay(100, {value: '🦄'});
// Executed after 100 milliseconds
console.log(result);
//=> '🦄'
})();
Using delay.reject()
, which optionally accepts a value and rejects it ms
later:
const delay = require('delay');
(async () => {
try {
await delay.reject(100, {value: new Error('🦄')});
console.log('This is never executed');
} catch (error) {
// 100 milliseconds later
console.log(error);
//=> [Error: 🦄]
}
})();
You can settle the delay early by calling .clear()
:
const delay = require('delay');
(async () => {
const delayedPromise = delay(1000, {value: 'Done'});
setTimeout(() => {
delayedPromise.clear();
}, 500);
// 500 milliseconds later
console.log(await delayedPromise);
//=> 'Done'
})();
You can abort the delay with an AbortSignal:
const delay = require('delay');
(async () => {
const abortController = new AbortController();
setTimeout(() => {
abortController.abort();
}, 500);
try {
await delay(1000, {signal: abortController.signal});
} catch (error) {
// 500 milliseconds later
console.log(error.name)
//=> 'AbortError'
}
})();
Related
- delay-cli - CLI for this module
- p-cancelable - Create a promise that can be canceled
- p-min-delay - Delay a promise a minimum amount of time
- p-immediate - Returns a promise resolved in the next event loop - think
setImmediate()
- p-timeout - Timeout a promise after a specified amount of time
- More…
License
MIT © Sindre Sorhus