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:
- 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

26
index.d.ts vendored
View File

@ -1,5 +1,9 @@
declare namespace pDefer {
interface DeferredPromise<ValueType> {
export interface DeferredPromise<ValueType> {
/**
The deferred promise.
*/
promise: Promise<ValueType>;
/**
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.
*/
reject(reason?: unknown): void;
/**
The deferred promise.
*/
promise: Promise<ValueType>;
}
}
/**
@ -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));
//=> '🦄'
})();
```
*/
declare function pDefer<ValueType>(): pDefer.DeferredPromise<ValueType>;
export = pDefer;
export default function pDefer<ValueType>(): DeferredPromise<ValueType>;

View File

@ -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;
}

View File

@ -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>());

View File

@ -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:

View File

@ -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"
}
}

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.
## 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));
//=> '🦄'
})();
```
*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">

View File

@ -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;
}