Skip to content
Timeout a promise after a specified amount of time
Branch: master
Clone or download
Latest commit c79394a Mar 12, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.editorconfig Meta tweaks Jul 2, 2017
.gitattributes
.gitignore Meta tweaks Jul 2, 2017
.npmrc Meta tweaks Jul 2, 2017
.travis.yml Require Node.js 8, add TypeScript definition (#10) Mar 12, 2019
index.d.ts Meta tweaks Mar 12, 2019
index.js Meta tweaks Mar 12, 2019
index.test-d.ts Require Node.js 8, add TypeScript definition (#10) Mar 12, 2019
license Meta tweaks Jul 2, 2017
package.json 3.0.0 Mar 12, 2019
readme.md
test.js Meta tweaks Mar 12, 2019

readme.md

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 | 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.

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

  • delay - Delay a promise a specified amount of time
  • p-min-delay - Delay a promise a minimum amount of time
  • p-retry - Retry a promise-returning function
  • More…

License

MIT © Sindre Sorhus

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.