ubdate
This commit is contained in:
36
node_modules/p-timeout/index.js
generated
vendored
Normal file
36
node_modules/p-timeout/index.js
generated
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
'use strict';
|
||||
const pFinally = require('p-finally');
|
||||
|
||||
class TimeoutError extends Error {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
this.name = 'TimeoutError';
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = (promise, ms, fallback) => new Promise((resolve, reject) => {
|
||||
if (typeof ms !== 'number' || ms < 0) {
|
||||
throw new TypeError('Expected `ms` to be a positive number');
|
||||
}
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
if (typeof fallback === 'function') {
|
||||
resolve(fallback());
|
||||
return;
|
||||
}
|
||||
|
||||
const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${ms} milliseconds`;
|
||||
const err = fallback instanceof Error ? fallback : new TimeoutError(message);
|
||||
|
||||
reject(err);
|
||||
}, ms);
|
||||
|
||||
pFinally(
|
||||
promise.then(resolve, reject),
|
||||
() => {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
module.exports.TimeoutError = TimeoutError;
|
||||
Reference in New Issue
Block a user