Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

p-timeout

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

p-timeout - npm Package Compare versions

Comparing version 4.0.1 to 4.1.0

15

index.d.ts

@@ -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;

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc