From f83ba3bb302f67d6e86395142167a9e49cb0b8f9 Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Fri, 9 Apr 2021 12:21:28 +0700 Subject: [PATCH] Require Node.js 12 and move to ESM --- .github/workflows/main.yml | 4 +--- index.d.ts | 48 +++++++++++++++++--------------------- index.js | 8 ++----- index.test-d.ts | 3 +-- license | 2 +- package.json | 13 +++++++---- readme.md | 17 ++++---------- test.js | 6 ++--- 8 files changed, 42 insertions(+), 59 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..d36e1a8 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -12,11 +12,9 @@ jobs: node-version: - 14 - 12 - - 10 - - 8 steps: - uses: actions/checkout@v2 - - uses: actions/setup-node@v1 + - uses: actions/setup-node@v2 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index aa6608f..12b65c6 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,24 +1,22 @@ -declare namespace pDefer { - interface DeferredPromise { - /** - Resolves the promise with a value or the result of another promise. +export interface DeferredPromise { + /** + The deferred promise. + */ + promise: Promise; - @param value - The value to resolve the promise with. - */ - resolve(value?: ValueType | PromiseLike): void; + /** + Resolves the promise with a value or the result of another promise. - /** - Reject the promise with a provided reason or error. + @param value - The value to resolve the promise with. + */ + resolve(value?: ValueType | PromiseLike): void; - @param reason - The reason or error to reject the promise with. - */ - reject(reason?: unknown): void; + /** + Reject the promise with a provided reason or error. - /** - The deferred promise. - */ - promise: Promise; - } + @param reason - The reason or error to reject the promise with. + */ + reject(reason?: unknown): void; } /** @@ -26,20 +24,16 @@ Create a deferred promise. @example ``` -import pDefer = require('p-defer'); +import pDefer from 'p-defer'; -function delay(ms) { +function delay(milliseconds) { const deferred = pDefer(); - setTimeout(deferred.resolve, ms, 'πŸ¦„'); + setTimeout(deferred.resolve, milliseconds, 'πŸ¦„'); return deferred.promise; } -(async () => { - console.log(await delay(100)); - //=> 'πŸ¦„' -})(); +console.log(await delay(100)); +//=> 'πŸ¦„' ``` */ -declare function pDefer(): pDefer.DeferredPromise; - -export = pDefer; +export default function pDefer(): DeferredPromise; diff --git a/index.js b/index.js index 81ff2d2..2670566 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,4 @@ -'use strict'; - -const pDefer = () => { +export default function pDefer() { const deferred = {}; deferred.promise = new Promise((resolve, reject) => { @@ -9,6 +7,4 @@ const pDefer = () => { }); return deferred; -}; - -module.exports = pDefer; +} diff --git a/index.test-d.ts b/index.test-d.ts index 08994d7..208757d 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,6 +1,5 @@ import {expectType} from 'tsd'; -import pDefer = require('.'); -import {DeferredPromise} from '.'; +import pDefer, {DeferredPromise} from './index.js'; expectType>(pDefer()); expectType>(pDefer()); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index deb70ad..1f5f548 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,16 @@ "description": "Create a deferred promise", "license": "MIT", "repository": "sindresorhus/p-defer", + "funding": "https://github.com/sponsors/sindresorhus", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=8" + "node": ">=12" }, "scripts": { "test": "xo && ava && tsd" @@ -32,8 +35,8 @@ "promises" ], "devDependencies": { - "ava": "^2.0.0", - "tsd": "^0.7.3", - "xo": "^0.24.0" + "ava": "^3.15.0", + "tsd": "^0.14.0", + "xo": "^0.38.2" } } diff --git a/readme.md b/readme.md index ff70118..4ae0503 100644 --- a/readme.md +++ b/readme.md @@ -4,47 +4,40 @@ [Don't use this unless you know what you're doing.](https://github.com/petkaantonov/bluebird/wiki/Promise-anti-patterns#the-deferred-anti-pattern) Prefer the `Promise` constructor. - ## Install ``` $ npm install p-defer ``` - ## Usage ```js -const pDefer = require('p-defer'); +import pDefer from 'p-defer'; -function delay(ms) { +function delay(milliseconds) { const deferred = pDefer(); - setTimeout(deferred.resolve, ms, 'πŸ¦„'); + setTimeout(deferred.resolve, milliseconds, 'πŸ¦„'); return deferred.promise; } -(async () => { - console.log(await delay(100)); - //=> 'πŸ¦„' -})(); +console.log(await delay(100)); +//=> 'πŸ¦„' ``` *The above is just an example. Use [`delay`](https://github.com/sindresorhus/delay) if you need to delay a promise.* - ## API ### pDefer() Returns an `object` with a `promise` property and functions to `resolve()` and `reject()`. - ## Related - [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise that defers execution until `.then()` or `.catch()` is called - [More…](https://github.com/sindresorhus/promise-fun) - ---
diff --git a/test.js b/test.js index 6e65a6c..dc1caba 100644 --- a/test.js +++ b/test.js @@ -1,11 +1,11 @@ import test from 'ava'; -import pDefer from '.'; +import pDefer from './index.js'; const fixture = Symbol('fixture'); -function delay(ms) { +function delay(milliseconds) { const deferred = pDefer(); - setTimeout(deferred.resolve, ms, fixture); + setTimeout(deferred.resolve, milliseconds, fixture); return deferred.promise; }