Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

promise-timeout

building51.1mMIT1.3.0TypeScript support: definitely-typed

Simple timeouts for promises

promise, timeout

readme

promise-timeout

A super-simple way to put a timeout on promise resolution.

It assumes you already have either platform support for promises (Node 0.12 or greater), or you have a polyfill (see es6-promise).

Installation

$ npm install promise-timeout

Usage

timeout(promise, timeoutMillis)

Rejects a promise with a TimeoutError if it does not settle within the specified timeout. Parameters:

  • promise: Promise - Promise to monitor.
  • timeoutMillis: number - Number of milliseconds to wait on settling.

TimeoutError

Exception indicating that the timeout expired.

Examples

ES2015:

import { timeout, TimeoutError } from 'promise-timeout';

let somePromise = goDoSomething();

timeout(somePromise, 1000)
  .then((thing) => console.log('I did a thing!'))
  .catch((err) => {
    if (err instanceof TimeoutError) {
      console.error('Timeout :-(');
    }
  });

ES5:

'use strict';

var pt = require('promise-timeout');

var somePromise = goDoSomething();

pt.timeout(somePromise, 1000)
  .then(function (thing) {
    console.log('I did a thing!');
  }).catch(function (err) {
    if (err instanceof pt.TimeoutError) {
      console.error('Timeout :-(');
    }
  });

changelog

promise-timeout

v1.3.0 (2018-02-18)

  • 3 - Make promise-timeout more portable

v1.2.0 (2018-02-08)

  • 4 - Improve the stack trace for TimeoutExceptions. Thanks, @tekwiz!

  • Add JsDoc for return value. Thanks again, @tekwiz!

v1.1.1 (2017-03-21)

  • 2 - Change let to var for ES5 compatibility. Thanks, @mateuscalza!

v1.1.0 (2017-03-07)

  • Updated mocha.
  • 1 - Clear timeout when promise settles, to avoid issues blocking the

    event loop from exiting.

v1.0.0 (2015-11-18)

  • Initial release