From 3249b2f0143ec79a571e0992c03739afbb2162ef Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Tue, 1 Dec 2020 22:50:36 +0700 Subject: [PATCH] Require Node.js 10 --- .github/workflows/main.yml | 1 - index.d.ts | 11 +++++------ index.js | 15 +++++++-------- index.test-d.ts | 9 +++++++-- package.json | 11 ++++------- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..c1870cf 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -13,7 +13,6 @@ jobs: - 14 - 12 - 10 - - 8 steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 diff --git a/index.d.ts b/index.d.ts index c0f624f..a9c1d37 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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, options?: pTimeout.Options ): Promise; - - TimeoutError: typeof TimeoutErrorClass; - - // TODO: Remove this for the next major release - default: typeof pTimeout; }; export = pTimeout; diff --git a/index.js b/index.js index e26438d..0a363ec 100644 --- a/index.js +++ b/index.js @@ -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; diff --git a/index.test-d.ts b/index.test-d.ts index a77942e..43cd998 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -2,8 +2,13 @@ import {expectType, expectError} from 'tsd'; import pTimeout = require('.'); import {TimeoutError} from '.'; -const delayedPromise: () => Promise = () => - new Promise(resolve => setTimeout(() => resolve('foo'), 200)); +const delayedPromise: () => Promise = async () => { + return new Promise(resolve => { + setTimeout(() => { + resolve('foo'); + }, 200); + }); +}; pTimeout(delayedPromise(), 50).then(() => 'foo'); pTimeout(delayedPromise(), 50, () => { diff --git a/package.json b/package.json index a506bfd..168e4f9 100644 --- a/package.json +++ b/package.json @@ -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" } }