Require Node.js 12 and move to ESM
This commit is contained in:
parent
857bca9cf5
commit
f83ba3bb30
|
@ -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
|
||||
|
|
|
@ -1,24 +1,22 @@
|
|||
declare namespace pDefer {
|
||||
interface DeferredPromise<ValueType> {
|
||||
/**
|
||||
Resolves the promise with a value or the result of another promise.
|
||||
export interface DeferredPromise<ValueType> {
|
||||
/**
|
||||
The deferred promise.
|
||||
*/
|
||||
promise: Promise<ValueType>;
|
||||
|
||||
@param value - The value to resolve the promise with.
|
||||
*/
|
||||
resolve(value?: ValueType | PromiseLike<ValueType>): 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<ValueType>): 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<ValueType>;
|
||||
}
|
||||
@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<ValueType>(): pDefer.DeferredPromise<ValueType>;
|
||||
|
||||
export = pDefer;
|
||||
export default function pDefer<ValueType>(): DeferredPromise<ValueType>;
|
||||
|
|
8
index.js
8
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;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
import {expectType} from 'tsd';
|
||||
import pDefer = require('.');
|
||||
import {DeferredPromise} from '.';
|
||||
import pDefer, {DeferredPromise} from './index.js';
|
||||
|
||||
expectType<DeferredPromise<unknown>>(pDefer());
|
||||
expectType<DeferredPromise<string>>(pDefer<string>());
|
||||
|
|
2
license
2
license
|
@ -1,6 +1,6 @@
|
|||
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:
|
||||
|
||||
|
|
13
package.json
13
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"
|
||||
}
|
||||
}
|
||||
|
|
17
readme.md
17
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)
|
||||
|
||||
|
||||
---
|
||||
|
||||
<div align="center">
|
||||
|
|
6
test.js
6
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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue