UNPKG

liveperson-functions-cli

Version:
24 lines 1.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.runTaskWithDeadline = void 0; const constants_1 = require("./constants"); const sdkError_1 = require("../types/errors/sdkError"); /** * Small helper functions that runs the provided task in race against a setTimeout with the specified * deadline. Further it will cleanup the timeout in any case. Ensuring it does not clock up. * @param task in form of an (still pending) promise * @param deadlineInMs time until task should be finished */ async function runTaskWithDeadline(task, deadlineInMs) { let timerID = undefined; // Required in order to allow cleanup of timeouts const deadline = new Promise((_, reject) => (timerID = setTimeout(reject, deadlineInMs, new sdkError_1.SDKError(constants_1.ErrorCodes.General.Timeout, `Request exceeded Deadline of ${deadlineInMs}ms`)))); try { const result = await Promise.race([task, deadline]); return result; } finally { clearTimeout(timerID); } } exports.runTaskWithDeadline = runTaskWithDeadline; //# sourceMappingURL=helper.js.map