Skip to content

Commit

Permalink
Require Node.js 12 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Apr 9, 2021
1 parent 857bca9 commit f83ba3b
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 63 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 25 additions & 31 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,39 @@
declare namespace pDefer {
interface DeferredPromise<ValueType> {
/**
Resolves the promise with a value or the result of another promise.
@param value - The value to resolve the promise with.
*/
resolve(value?: ValueType | PromiseLike<ValueType>): void;

/**
Reject the promise with a provided reason or error.
@param reason - The reason or error to reject the promise with.
*/
reject(reason?: unknown): void;

/**
The deferred promise.
*/
promise: Promise<ValueType>;
}
export interface DeferredPromise<ValueType> {
/**
The deferred promise.
*/
promise: Promise<ValueType>;

/**
Resolves the promise with a value or the result of another promise.
@param value - The value to resolve the promise with.
*/
resolve(value?: ValueType | PromiseLike<ValueType>): void;

/**
Reject the promise with a provided reason or error.
@param reason - The reason or error to reject the promise with.
*/
reject(reason?: unknown): void;
}

/**
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 changes: 2 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
'use strict';

const pDefer = () => {
export default function pDefer() {
const deferred = {};

deferred.promise = new Promise((resolve, reject) => {
Expand All @@ -9,6 +7,4 @@ const pDefer = () => {
});

return deferred;
};

module.exports = pDefer;
}
3 changes: 1 addition & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -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>());
Expand Down
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -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:

Expand Down
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 changes: 5 additions & 12 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
6 changes: 3 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import test from 'ava';
import pDefer from '.';
import pDefer from './index.js';

This comment has been minimized.

Copy link
@papb

papb Jul 8, 2021

Why change '.' to './index.js'?

This comment has been minimized.

Copy link
@sindresorhus

sindresorhus Jul 8, 2021

Author Owner

It’s required for ESM.


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

Expand Down

1 comment on commit f83ba3b

@fregante
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for keeping this module a 10-lines module without dependencies 😁

Please sign in to comment.