sleep-cancel
v1.0.0
Published
Cancellable sleep function for Typescript
Downloads
9
Maintainers
Readme
sleep-cancel: Cancellable sleep function for Typescript
A small, simple, promise-based, cancellable delay function for Typescript and Javascript.
Installation
npm install sleep-cancel
Basic Usage
import sleep from 'sleep-cancel';
(async function () {
console.log('Sleeping at', Date.now());
await sleep(2000);
console.log('Woke at', Date.now());
})();
Cancellation Usage
import sleep, { SleepCancelled } from 'sleep-cancel';
const promise = sleep(2000);
(async function () {
console.log('Sleeping at', Date.now());
try {
await promise;
} catch (e) {
if (e instanceof SleepCancelled) {
console.log('Sleep cancelled')
} else {
throw e;
}
} finally {
console.log('Woke at', Date.now());
}
})();
promise.cancel();
License
sleep-cancel
is available under the ISC license.