Comparing version 4.0.1 to 4.1.0
@@ -43,2 +43,9 @@ declare class TimeoutErrorClass extends Error { | ||
interface ClearablePromise<T> extends Promise<T>{ | ||
/** | ||
Clear the timeout. | ||
*/ | ||
clear: () => void; | ||
} | ||
declare const pTimeout: { | ||
@@ -57,3 +64,3 @@ TimeoutError: typeof TimeoutErrorClass; | ||
@param message - Specify a custom error message or error. If you do a custom error, it's recommended to sub-class `pTimeout.TimeoutError`. Default: `'Promise timed out after 50 milliseconds'`. | ||
@returns A decorated `input` that times out after `milliseconds` time. | ||
@returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout. | ||
@@ -76,3 +83,3 @@ @example | ||
options?: pTimeout.Options | ||
): Promise<ValueType>; | ||
): ClearablePromise<ValueType>; | ||
@@ -87,3 +94,3 @@ /** | ||
@param fallback - Do something other than rejecting with an error on timeout. You could for example retry. | ||
@returns A decorated `input` that times out after `milliseconds` time. | ||
@returns A decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout. | ||
@@ -107,5 +114,5 @@ @example | ||
options?: pTimeout.Options | ||
): Promise<ValueType | ReturnType>; | ||
): ClearablePromise<ValueType | ReturnType>; | ||
}; | ||
export = pTimeout; |
84
index.js
@@ -10,49 +10,59 @@ 'use strict'; | ||
const pTimeout = (promise, milliseconds, fallback, options) => new Promise((resolve, reject) => { | ||
if (typeof milliseconds !== 'number' || milliseconds < 0) { | ||
throw new TypeError('Expected `milliseconds` to be a positive number'); | ||
} | ||
const pTimeout = (promise, milliseconds, fallback, options) => { | ||
let timer; | ||
const cancelablePromise = new Promise((resolve, reject) => { | ||
if (typeof milliseconds !== 'number' || milliseconds < 0) { | ||
throw new TypeError('Expected `milliseconds` to be a positive number'); | ||
} | ||
if (milliseconds === Infinity) { | ||
resolve(promise); | ||
return; | ||
} | ||
if (milliseconds === Infinity) { | ||
resolve(promise); | ||
return; | ||
} | ||
options = { | ||
customTimers: {setTimeout, clearTimeout}, | ||
...options | ||
}; | ||
options = { | ||
customTimers: {setTimeout, clearTimeout}, | ||
...options | ||
}; | ||
const timer = options.customTimers.setTimeout.call(undefined, () => { | ||
if (typeof fallback === 'function') { | ||
timer = options.customTimers.setTimeout.call(undefined, () => { | ||
if (typeof fallback === 'function') { | ||
try { | ||
resolve(fallback()); | ||
} catch (error) { | ||
reject(error); | ||
} | ||
return; | ||
} | ||
const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${milliseconds} milliseconds`; | ||
const timeoutError = fallback instanceof Error ? fallback : new TimeoutError(message); | ||
if (typeof promise.cancel === 'function') { | ||
promise.cancel(); | ||
} | ||
reject(timeoutError); | ||
}, milliseconds); | ||
(async () => { | ||
try { | ||
resolve(fallback()); | ||
resolve(await promise); | ||
} catch (error) { | ||
reject(error); | ||
} finally { | ||
options.customTimers.clearTimeout.call(undefined, timer); | ||
} | ||
})(); | ||
}); | ||
return; | ||
} | ||
cancelablePromise.clear = () => { | ||
clearTimeout(timer); | ||
timer = undefined; | ||
}; | ||
const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${milliseconds} milliseconds`; | ||
const timeoutError = fallback instanceof Error ? fallback : new TimeoutError(message); | ||
return cancelablePromise; | ||
}; | ||
if (typeof promise.cancel === 'function') { | ||
promise.cancel(); | ||
} | ||
reject(timeoutError); | ||
}, milliseconds); | ||
(async () => { | ||
try { | ||
resolve(await promise); | ||
} catch (error) { | ||
reject(error); | ||
} finally { | ||
options.customTimers.clearTimeout.call(undefined, timer); | ||
} | ||
})(); | ||
}); | ||
module.exports = pTimeout; | ||
@@ -59,0 +69,0 @@ // TODO: Remove this for the next major release |
{ | ||
"name": "p-timeout", | ||
"version": "4.0.1", | ||
"version": "4.1.0", | ||
"description": "Timeout a promise after a specified amount of time", | ||
@@ -40,4 +40,6 @@ "license": "MIT", | ||
"tsd": "^0.13.1", | ||
"xo": "^0.35.0" | ||
"xo": "^0.35.0", | ||
"in-range": "^2.0.0", | ||
"time-span": "^4.0.0" | ||
} | ||
} |
@@ -28,3 +28,3 @@ # p-timeout | ||
Returns a decorated `input` that times out after `milliseconds` time. | ||
Returns a decorated `input` that times out after `milliseconds` time. It has a `.clear()` method that clears the timeout. | ||
@@ -31,0 +31,0 @@ 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. |
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
9424
147
7