Require Node.js 12 and move to ESM

This commit is contained in:
Sindre Sorhus 2021-04-09 12:21:28 +07:00
parent 857bca9cf5
commit f83ba3bb30
8 changed files with 42 additions and 59 deletions

View File

@ -12,11 +12,9 @@ jobs:
node-version: node-version:
- 14 - 14
- 12 - 12
- 10
- 8
steps: steps:
- uses: actions/checkout@v2 - uses: actions/checkout@v2
- uses: actions/setup-node@v1 - uses: actions/setup-node@v2
with: with:
node-version: ${{ matrix.node-version }} node-version: ${{ matrix.node-version }}
- run: npm install - run: npm install

26
index.d.ts vendored
View File

@ -1,5 +1,9 @@
declare namespace pDefer { export interface DeferredPromise<ValueType> {
interface DeferredPromise<ValueType> { /**
The deferred promise.
*/
promise: Promise<ValueType>;
/** /**
Resolves the promise with a value or the result of another promise. Resolves the promise with a value or the result of another promise.
@ -13,12 +17,6 @@ declare namespace pDefer {
@param reason - The reason or error to reject the promise with. @param reason - The reason or error to reject the promise with.
*/ */
reject(reason?: unknown): void; reject(reason?: unknown): void;
/**
The deferred promise.
*/
promise: Promise<ValueType>;
}
} }
/** /**
@ -26,20 +24,16 @@ Create a deferred promise.
@example @example
``` ```
import pDefer = require('p-defer'); import pDefer from 'p-defer';
function delay(ms) { function delay(milliseconds) {
const deferred = pDefer(); const deferred = pDefer();
setTimeout(deferred.resolve, ms, '🦄'); setTimeout(deferred.resolve, milliseconds, '🦄');
return deferred.promise; return deferred.promise;
} }
(async () => {
console.log(await delay(100)); console.log(await delay(100));
//=> '🦄' //=> '🦄'
})();
``` ```
*/ */
declare function pDefer<ValueType>(): pDefer.DeferredPromise<ValueType>; export default function pDefer<ValueType>(): DeferredPromise<ValueType>;
export = pDefer;

View File

@ -1,6 +1,4 @@
'use strict'; export default function pDefer() {
const pDefer = () => {
const deferred = {}; const deferred = {};
deferred.promise = new Promise((resolve, reject) => { deferred.promise = new Promise((resolve, reject) => {
@ -9,6 +7,4 @@ const pDefer = () => {
}); });
return deferred; return deferred;
}; }
module.exports = pDefer;

View File

@ -1,6 +1,5 @@
import {expectType} from 'tsd'; import {expectType} from 'tsd';
import pDefer = require('.'); import pDefer, {DeferredPromise} from './index.js';
import {DeferredPromise} from '.';
expectType<DeferredPromise<unknown>>(pDefer()); expectType<DeferredPromise<unknown>>(pDefer());
expectType<DeferredPromise<string>>(pDefer<string>()); expectType<DeferredPromise<string>>(pDefer<string>());

View File

@ -1,6 +1,6 @@
MIT License MIT License
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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: 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:

View File

@ -4,13 +4,16 @@
"description": "Create a deferred promise", "description": "Create a deferred promise",
"license": "MIT", "license": "MIT",
"repository": "sindresorhus/p-defer", "repository": "sindresorhus/p-defer",
"funding": "https://github.com/sponsors/sindresorhus",
"author": { "author": {
"name": "Sindre Sorhus", "name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com", "email": "sindresorhus@gmail.com",
"url": "sindresorhus.com" "url": "https://sindresorhus.com"
}, },
"type": "module",
"exports": "./index.js",
"engines": { "engines": {
"node": ">=8" "node": ">=12"
}, },
"scripts": { "scripts": {
"test": "xo && ava && tsd" "test": "xo && ava && tsd"
@ -32,8 +35,8 @@
"promises" "promises"
], ],
"devDependencies": { "devDependencies": {
"ava": "^2.0.0", "ava": "^3.15.0",
"tsd": "^0.7.3", "tsd": "^0.14.0",
"xo": "^0.24.0" "xo": "^0.38.2"
} }
} }

View File

@ -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. [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 ## Install
``` ```
$ npm install p-defer $ npm install p-defer
``` ```
## Usage ## Usage
```js ```js
const pDefer = require('p-defer'); import pDefer from 'p-defer';
function delay(ms) { function delay(milliseconds) {
const deferred = pDefer(); const deferred = pDefer();
setTimeout(deferred.resolve, ms, '🦄'); setTimeout(deferred.resolve, milliseconds, '🦄');
return deferred.promise; 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.* *The above is just an example. Use [`delay`](https://github.com/sindresorhus/delay) if you need to delay a promise.*
## API ## API
### pDefer() ### pDefer()
Returns an `object` with a `promise` property and functions to `resolve()` and `reject()`. Returns an `object` with a `promise` property and functions to `resolve()` and `reject()`.
## Related ## Related
- [p-lazy](https://github.com/sindresorhus/p-lazy) - Create a lazy promise that defers execution until `.then()` or `.catch()` is called - [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) - [More…](https://github.com/sindresorhus/promise-fun)
--- ---
<div align="center"> <div align="center">

View File

@ -1,11 +1,11 @@
import test from 'ava'; import test from 'ava';
import pDefer from '.'; import pDefer from './index.js';
const fixture = Symbol('fixture'); const fixture = Symbol('fixture');
function delay(ms) { function delay(milliseconds) {
const deferred = pDefer(); const deferred = pDefer();
setTimeout(deferred.resolve, ms, fixture); setTimeout(deferred.resolve, milliseconds, fixture);
return deferred.promise; return deferred.promise;
} }