Skip to content

ValorLin/timeout-lib

Repository files navigation

CI

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.

About

A timeout lib used for handle async function.

Resources

License

Stars

Watchers

Forks

Packages

No packages published