method-timeout-rejection

1.0.0 • Public • Published

method timeout rejection

Reject method (encapsulate in a promise) if it takes more than a set duration

Usage

Constructor defines a timeout in milliseconds.

const MethodTimeoutRejection = require('method-timeout-rejection');
 
const methodTimeout = new MethodTimeoutRejection(1000);

Method timeoutRejection wraps a callback in a promise and define a timeout in milliseconds. If the callback return something in that time then it acts in the exact same way as the original callback, otherwise it's rejected with a specific MethodTimeOutError error.

methodTimeout.timeoutRejection((callback) => {
  setTimeout(() => {
    callback(null, 'hello');
  }, 2000);
})
.catch((err) => {
  assert(err instanceof MethodTimeoutRejection.MethodTimeOutError);
});

Method hasExpired just returns true if inside the timeoutRejection method, the global callback has timeout.

Package Sidebar

Install

npm i method-timeout-rejection

Weekly Downloads

4

Version

1.0.0

License

MIT

Last publish

Collaborators

  • mathieutamer