timeout-lib
v0.0.4
Published
![CI](https://github.com/weilao/timeout-lib/workflows/CI/badge.svg)
Downloads
1
Readme
Timeout Lib
Install
// Use npm
$ npm install timeout-lib
// Or use yarn
$ yarn add timeout-lib
Usage
withTimeout()
import { withTimeout, TimeoutError } from 'timeout-lib';
const _fn = async () => {
await new Promise(() => {});
}
const fn = withTimeout(_fn, { time: 1000 });
try {
await fn();
} catch(err) {
if(err instanceof TimeoutError) {
console.log(`Got a TimeoutError while executing fn()`)
}
}
@timeout()
import { timeout, TimeoutError } from 'timeout-lib';
class Demo {
@timeout({ time: 1000 })
async fn() {
await new Promise(() => {});
}
}
try {
await new Demo().fn();
} catch(err) {
if(err instanceof TimeoutError) {
console.log(`Got a TimeoutError while executing fn()`)
}
}
License
MIT.