Upgrade Ava to v4
This commit is contained in:
parent
9a40cc5274
commit
ce89f1b611
1153 changed files with 27264 additions and 95308 deletions
90
node_modules/p-timeout/index.js
generated
vendored
90
node_modules/p-timeout/index.js
generated
vendored
|
|
@ -1,57 +1,63 @@
|
|||
'use strict';
|
||||
|
||||
const pFinally = require('p-finally');
|
||||
|
||||
class TimeoutError extends Error {
|
||||
export class TimeoutError extends Error {
|
||||
constructor(message) {
|
||||
super(message);
|
||||
this.name = 'TimeoutError';
|
||||
}
|
||||
}
|
||||
|
||||
const pTimeout = (promise, milliseconds, fallback) => new Promise((resolve, reject) => {
|
||||
if (typeof milliseconds !== 'number' || milliseconds < 0) {
|
||||
throw new TypeError('Expected `milliseconds` to be a positive number');
|
||||
}
|
||||
|
||||
if (milliseconds === Infinity) {
|
||||
resolve(promise);
|
||||
return;
|
||||
}
|
||||
|
||||
const timer = setTimeout(() => {
|
||||
if (typeof fallback === 'function') {
|
||||
try {
|
||||
resolve(fallback());
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
export default function pTimeout(promise, milliseconds, fallback, options) {
|
||||
let timer;
|
||||
const cancelablePromise = new Promise((resolve, reject) => {
|
||||
if (typeof milliseconds !== 'number' || Math.sign(milliseconds) !== 1) {
|
||||
throw new TypeError(`Expected \`milliseconds\` to be a positive number, got \`${milliseconds}\``);
|
||||
}
|
||||
|
||||
if (milliseconds === Number.POSITIVE_INFINITY) {
|
||||
resolve(promise);
|
||||
return;
|
||||
}
|
||||
|
||||
const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${milliseconds} milliseconds`;
|
||||
const timeoutError = fallback instanceof Error ? fallback : new TimeoutError(message);
|
||||
options = {
|
||||
customTimers: {setTimeout, clearTimeout},
|
||||
...options
|
||||
};
|
||||
|
||||
if (typeof promise.cancel === 'function') {
|
||||
promise.cancel();
|
||||
}
|
||||
timer = options.customTimers.setTimeout.call(undefined, () => {
|
||||
if (typeof fallback === 'function') {
|
||||
try {
|
||||
resolve(fallback());
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
}
|
||||
|
||||
reject(timeoutError);
|
||||
}, milliseconds);
|
||||
return;
|
||||
}
|
||||
|
||||
// TODO: Use native `finally` keyword when targeting Node.js 10
|
||||
pFinally(
|
||||
// eslint-disable-next-line promise/prefer-await-to-then
|
||||
promise.then(resolve, reject),
|
||||
() => {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
);
|
||||
});
|
||||
const message = typeof fallback === 'string' ? fallback : `Promise timed out after ${milliseconds} milliseconds`;
|
||||
const timeoutError = fallback instanceof Error ? fallback : new TimeoutError(message);
|
||||
|
||||
module.exports = pTimeout;
|
||||
// TODO: Remove this for the next major release
|
||||
module.exports.default = pTimeout;
|
||||
if (typeof promise.cancel === 'function') {
|
||||
promise.cancel();
|
||||
}
|
||||
|
||||
module.exports.TimeoutError = TimeoutError;
|
||||
reject(timeoutError);
|
||||
}, milliseconds);
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
resolve(await promise);
|
||||
} catch (error) {
|
||||
reject(error);
|
||||
} finally {
|
||||
options.customTimers.clearTimeout.call(undefined, timer);
|
||||
}
|
||||
})();
|
||||
});
|
||||
|
||||
cancelablePromise.clear = () => {
|
||||
clearTimeout(timer);
|
||||
timer = undefined;
|
||||
};
|
||||
|
||||
return cancelablePromise;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue