Socket
Book a DemoInstallSign in
Socket

timeoutable-wrapper

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

timeoutable-wrapper

Signals timeout of the wrapped function by throwing TimeoutError

latest
Source
npmnpm
Version
0.2.1
Version published
Maintainers
1
Created
Source

timeoutable-wrapper

Build Status Coverage Status

A wrapper for a potentially long-running task to signal it's timed out. Does not halt the wrapped task, only signals it's timed out (it's your business how to react on the timeout) by throwing TimeoutError. Supports both promises and node-style callbacks.

Install

    npm i --save timeoutable-wrapper

Usage


const timeoutable = require('timeoutable-wrapper')

const potentiallyLongRunningTask = async function (arg1, arg2) {
    /* some long-running code */
    return result
}

// or, with callback
const anotherPotentiallyLongRunningTask = function (arg1, arg2, cb) {
    /* some long-running code */
    cb(null, result)
}

const timeoutableTask = timeoutable(potentiallyLongRunningTask, 5000)
const anotherTimeoutableTask = timeoutable(anotherPotentiallyLongRunningTask, 5000)

try {

    // if task is not completed within 5000 msecs - throw TimeoutError
    const result = await timeoutableTask(arg1, arg2)

    // if another task is not called callback within 5000 msecs - throw TimeoutError
    const result2 = await anotherTimeoutableTask(arg1, arg2)

} catch (err) {
    if (err.name === 'TimeoutError') { // or err instanceof timeoutable.TimeoutError
        // it's timeout
    } else {
        // it's something else
    }
}

API

timeoutable (fn, ms) -> Function

Wraps passed function and returns another one, which throws TimeoutError when specified ms expired before fn returned result.

timeoutable.using(PromiseLib) -> timeoutable

Returns an instance of timeoutable-wrapper that uses specified PromiseLib to construct and return promises from the lib.

timeoutable.TimeoutError

Type for errors thrown on expired timeouts.

Keywords

timeout

FAQs

Package last updated on 05 Mar 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts