UNPKG

es-promise-ext

Version:

Native promise extensions for javascript and typescript.

19 lines (18 loc) 972 B
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = timeOut; const delay_1 = __importDefault(require("../../delay/delay")); function timeOut(asyncFunction, millisecond = 1000) { if (!(asyncFunction instanceof Promise) && typeof asyncFunction !== 'function') throw TypeError('Promise.timeOut parameter 1 must be an async function or Promise'); if (!Number.isInteger(millisecond) || millisecond < 0) throw TypeError('Promise.timeOut parameter 2 must be a positive integer'); const promise = typeof asyncFunction === 'function' ? asyncFunction() : asyncFunction; return () => Promise.race([ promise, (0, delay_1.default)(millisecond).then(() => Promise.reject(new Error(`Time Out Error: promise can not resolve in ${millisecond}ms`))) ]); }