es-promise-ext
Version:
Native promise extensions for javascript and typescript.
42 lines (41 loc) • 1.39 kB
TypeScript
import { AsyncFunction } from './timeOut';
declare global {
interface PromiseConstructor {
/**
* Call the async function with time out limit.
*
* @param {AsyncFunction<T>} asyncFunction
* - an async function
*
* @param {number} [millisecond=1000]
* - the time limit for the time out
*
* @return {Promise<T> | Promise<never>}
* The returned promise
*
* @example
* Promise.timeOut(asyncFunction, 300)
* .then(doSomething)
* // return a promise within 300 ms, otherwise reject with time out error
*/
timeOut<T>(asyncFunction: AsyncFunction<T>, millisecond?: number): Promise<T> | Promise<never>;
/**
* Return the promise with time out limit.
*
* @param {Promise<T>} promise
* - a promise
*
* @param {number} [millisecond=1000]
* - the time limit for the time out
*
* @return {Promise<T> | Promise<never>}
* The returned promise
*
* @example
* Promise.timeOut(promise, 300)
* .then(doSomething)
* // return a promise within 300 ms, otherwise reject with time out error
*/
timeOut<T>(promise: Promise<T>, millisecond?: number): Promise<T> | Promise<never>;
}
}