Require Node.js 10

This commit is contained in:
Sindre Sorhus 2020-12-01 22:50:36 +07:00
parent 085f437e49
commit 3249b2f014
5 changed files with 23 additions and 24 deletions

View File

@ -13,7 +13,6 @@ jobs:
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1

11
index.d.ts vendored
View File

@ -37,10 +37,14 @@ declare namespace pTimeout {
setTimeout: typeof global.setTimeout;
clearTimeout: typeof global.clearTimeout;
};
}
};
}
declare const pTimeout: {
TimeoutError: typeof TimeoutErrorClass;
default: typeof pTimeout;
/**
Timeout a promise after a specified amount of time.
@ -97,11 +101,6 @@ declare const pTimeout: {
fallback: () => ReturnType | Promise<ReturnType>,
options?: pTimeout.Options
): Promise<ValueType | ReturnType>;
TimeoutError: typeof TimeoutErrorClass;
// TODO: Remove this for the next major release
default: typeof pTimeout;
};
export = pTimeout;

View File

@ -1,7 +1,5 @@
'use strict';
const pFinally = require('p-finally');
class TimeoutError extends Error {
constructor(message) {
super(message);
@ -45,14 +43,15 @@ const pTimeout = (promise, milliseconds, fallback, options) => new Promise((reso
reject(timeoutError);
}, milliseconds);
// TODO: Use native `finally` keyword when targeting Node.js 10
pFinally(
// eslint-disable-next-line promise/prefer-await-to-then
promise.then(resolve, reject),
() => {
(async () => {
try {
resolve(await promise);
} catch (error) {
reject(error);
} finally {
options.customTimers.clearTimeout(timer);
}
);
})();
});
module.exports = pTimeout;

View File

@ -2,8 +2,13 @@ import {expectType, expectError} from 'tsd';
import pTimeout = require('.');
import {TimeoutError} from '.';
const delayedPromise: () => Promise<string> = () =>
new Promise(resolve => setTimeout(() => resolve('foo'), 200));
const delayedPromise: () => Promise<string> = async () => {
return new Promise(resolve => {
setTimeout(() => {
resolve('foo');
}, 200);
});
};
pTimeout(delayedPromise(), 50).then(() => 'foo');
pTimeout(delayedPromise(), 50, () => {

View File

@ -10,7 +10,7 @@
"url": "https://sindresorhus.com"
},
"engines": {
"node": ">=8"
"node": ">=10"
},
"scripts": {
"test": "xo && ava && tsd"
@ -32,14 +32,11 @@
"cancel",
"bluebird"
],
"dependencies": {
"p-finally": "^1.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"delay": "^4.1.0",
"ava": "^2.4.0",
"delay": "^4.4.0",
"p-cancelable": "^2.0.0",
"tsd": "^0.13.1",
"xo": "^0.24.0"
"xo": "^0.35.0"
}
}