timeoutable-wrapper
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 = const potentiallyLongRunningTask = { /* some long-running code */ return result} // or, with callbackconst anotherPotentiallyLongRunningTask = { /* some long-running code */ } const timeoutableTask = const anotherTimeoutableTask = try // if task is not completed within 5000 msecs - throw TimeoutError const result = await // if another task is not called callback within 5000 msecs - throw TimeoutError const result2 = await catch err if errname === '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.