UNPKG

ts-prime

Version:

A utility library for JavaScript and Typescript.

30 lines (29 loc) 882 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var index_1 = require("./index"); var purry_1 = require("./purry"); function timeout() { return purry_1.purry(_timeout, arguments); } exports.timeout = timeout; function _timeout(fn, maxDuration) { return function () { var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i] = arguments[_i]; } return Promise.race([ new Promise(function (resolve) { setTimeout(function () { resolve(new Error((fn.name + " timeout after " + maxDuration + "ms").trim())); }, maxDuration); }), fn.apply(void 0, args), ]).then(function (q) { if (index_1.isError(q)) { throw q; } return q; }); }; }