p-timeout
Timeout a promise after a specified amount of time
Last updated 8 years ago by sindresorhus .
MIT · Repository · Bugs · Original npm
$ cnpm install p-timeout 
SYNC missed versions from official npm registry.

p-timeout Build Status

Timeout a promise after a specified amount of time

Install

$ npm install p-timeout

Usage

const delay = require('delay');
const pTimeout = require('p-timeout');

const delayedPromise = delay(200);

pTimeout(delayedPromise, 50).then(() => 'foo');
//=> [TimeoutError: Promise timed out after 50 milliseconds]

API

pTimeout(input, milliseconds, message?)

pTimeout(input, milliseconds, fallback?)

Returns a decorated input that times out after milliseconds time.

If you pass in a cancelable promise, specifically a promise with a .cancel() method, that method will be called when the pTimeout promise times out.

input

Type: Promise

Promise to decorate.

milliseconds

Type: number

Milliseconds before timing out.

Passing Infinity will cause it to never time out.

message

Type: string Error
Default: 'Promise timed out after 50 milliseconds'

Specify a custom error message or error.

If you do a custom error, it's recommended to sub-class pTimeout.TimeoutError.

fallback

Type: Function

Do something other than rejecting with an error on timeout.

You could for example retry:

const delay = require('delay');
const pTimeout = require('p-timeout');

const delayedPromise = () => delay(200);

pTimeout(delayedPromise(), 50, () => {
	return pTimeout(delayedPromise(), 300);
});

pTimeout.TimeoutError

Exposed for instance checking and sub-classing.

Related

Current Tags

  • 6.0.0                                ...           latest (3 years ago)

7 Versions

  • 6.0.0                                ...           3 years ago
  • 5.1.0                                ...           3 years ago
  • 4.1.0                                ...           5 years ago
  • 5.0.2                                ...           4 years ago
  • 2.0.1                                ...           8 years ago
  • 1.2.1                                ...           8 years ago
  • 3.2.0                                ...           6 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (1)
Dev Dependencies (3)
Dependents (0)
None

Copyright 2013 - present © cnpmjs.org