Comparing version 2.0.1 to 3.0.0
25
index.js
'use strict'; | ||
const pFinally = require('p-finally'); | ||
@@ -11,5 +12,5 @@ | ||
module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => { | ||
if (typeof ms !== 'number' || ms < 0) { | ||
throw new TypeError('Expected `ms` to be a positive number'); | ||
const pTimeout = (promise, milliseconds, fallback) => new Promise((resolve, reject) => { | ||
if (typeof milliseconds !== 'number' || milliseconds < 0) { | ||
throw new TypeError('Expected `milliseconds` to be a positive number'); | ||
} | ||
@@ -21,10 +22,11 @@ | ||
resolve(fallback()); | ||
} catch (err) { | ||
reject(err); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
return; | ||
} | ||
const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${ms} milliseconds`; | ||
const err = fallback instanceof Error ? fallback : new TimeoutError(message); | ||
const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${milliseconds} milliseconds`; | ||
const timeoutError = fallback instanceof Error ? fallback : new TimeoutError(message); | ||
@@ -35,6 +37,8 @@ if (typeof promise.cancel === 'function') { | ||
reject(err); | ||
}, ms); | ||
reject(timeoutError); | ||
}, milliseconds); | ||
// TODO: Use native `finally` keyword when targeting Node.js 10 | ||
pFinally( | ||
// eslint-disable-next-line promise/prefer-await-to-then | ||
promise.then(resolve, reject), | ||
@@ -47,2 +51,5 @@ () => { | ||
module.exports = pTimeout; | ||
module.exports.default = pTimeout; | ||
module.exports.TimeoutError = TimeoutError; |
{ | ||
"name": "p-timeout", | ||
"version": "2.0.1", | ||
"description": "Timeout a promise after a specified amount of time", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-timeout", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava" | ||
}, | ||
"files": [ | ||
"index.js" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"timeout", | ||
"error", | ||
"invalidate", | ||
"async", | ||
"await", | ||
"promises", | ||
"time", | ||
"out", | ||
"cancel", | ||
"bluebird" | ||
], | ||
"dependencies": { | ||
"p-finally": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "*", | ||
"delay": "^2.0.0", | ||
"p-cancelable": "^0.3.0", | ||
"xo": "*" | ||
} | ||
"name": "p-timeout", | ||
"version": "3.0.0", | ||
"description": "Timeout a promise after a specified amount of time", | ||
"license": "MIT", | ||
"repository": "sindresorhus/p-timeout", | ||
"author": { | ||
"name": "Sindre Sorhus", | ||
"email": "sindresorhus@gmail.com", | ||
"url": "sindresorhus.com" | ||
}, | ||
"engines": { | ||
"node": ">=8" | ||
}, | ||
"scripts": { | ||
"test": "xo && ava && tsd-check" | ||
}, | ||
"files": [ | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
"keywords": [ | ||
"promise", | ||
"timeout", | ||
"error", | ||
"invalidate", | ||
"async", | ||
"await", | ||
"promises", | ||
"time", | ||
"out", | ||
"cancel", | ||
"bluebird" | ||
], | ||
"dependencies": { | ||
"p-finally": "^1.0.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^1.3.1", | ||
"delay": "^4.1.0", | ||
"p-cancelable": "^1.1.0", | ||
"tsd-check": "^0.3.0", | ||
"xo": "^0.24.0" | ||
} | ||
} |
@@ -28,5 +28,5 @@ # p-timeout [![Build Status](https://travis-ci.org/sindresorhus/p-timeout.svg?branch=master)](https://travis-ci.org/sindresorhus/p-timeout) | ||
### pTimeout(input, ms, [message | fallback]) | ||
### pTimeout(input, milliseconds, [message | fallback]) | ||
Returns a decorated `input` that times out after `ms` time. | ||
Returns a decorated `input` that times out after `milliseconds` time. | ||
@@ -41,3 +41,3 @@ 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. | ||
#### ms | ||
#### milliseconds | ||
@@ -44,0 +44,0 @@ Type: `number` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6792
5
85
5