knex/lib/util/timeout.js

30 lines
639 B
JavaScript
Raw Normal View History

2020-02-12 23:42:15 +03:00
class KnexTimeoutError extends Error {
constructor(message) {
super(message);
this.name = 'KnexTimeoutError';
}
}
function timeout(promise, ms) {
2020-04-19 00:40:23 +02:00
return new Promise(function (resolve, reject) {
const id = setTimeout(function () {
reject(new KnexTimeoutError('operation timed out'));
}, ms);
function wrappedResolve(value) {
clearTimeout(id);
resolve(value);
}
function wrappedReject(err) {
clearTimeout(id);
reject(err);
}
promise.then(wrappedResolve, wrappedReject);
});
}
2020-02-12 23:42:15 +03:00
module.exports.KnexTimeoutError = KnexTimeoutError;
module.exports.timeout = timeout;