Repository URL to install this package:
|
Version:
5.0.0-1 ▾
|
vending-agent-dep
/
usr
/
local
/
lib
/
vending
/
agent
/
node_modules
/
native-promise-util
/
dist
/
timeout.js
|
|---|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.timeout = void 0;
const errors_1 = require("./errors");
const ERRMSG_TIMEOUT_ERROR = 'operation timed out';
function buildTimeoutError(messageOrError) {
let err;
if (typeof messageOrError !== 'string') {
if (messageOrError instanceof Error) {
err = messageOrError;
}
else {
err = new errors_1.TimeoutError(ERRMSG_TIMEOUT_ERROR);
}
}
else {
err = new errors_1.TimeoutError(messageOrError);
}
return err;
}
// eslint-disable-next-line @typescript-eslint/promise-function-async
function timeout(ms, messageOrError, value) {
if (value === undefined) {
// eslint-disable-next-line promise/param-names, @typescript-eslint/return-await
return new Promise((_resolve, reject) => setTimeout(() => {
reject(buildTimeoutError(messageOrError));
}, ms));
}
return new Promise((resolve, reject) => {
const timer = setTimeout(() => {
reject(buildTimeoutError(messageOrError));
}, ms);
(async () => {
try {
const resolvedValue = await value;
resolve(resolvedValue);
}
catch (error) {
reject(error);
}
finally {
clearTimeout(timer);
}
})().catch(() => { });
});
}
exports.timeout = timeout;