Compare commits

...

11 Commits
v2.0.1 ... main

Author SHA1 Message Date
Derrick Hammer 534ba472a8
refactor: convert to cjs 2023-04-23 08:08:27 -04:00
Sindre Sorhus 0abf7f5633 4.0.0 2021-04-09 12:22:23 +07:00
Sindre Sorhus f83ba3bb30 Require Node.js 12 and move to ESM 2021-04-09 12:21:28 +07:00
Richie Bendall 857bca9cf5
Move to GitHub Actions (#6) 2021-01-01 17:41:38 +07:00
Sindre Sorhus b59e39440b Tidelift tasks 2019-09-10 13:12:12 +07:00
Sindre Sorhus 11f2980f79 3.0.0 2019-06-07 15:10:46 +07:00
Sindre Sorhus 71b64de6d6 Require Node.js 8 2019-06-07 15:08:42 +07:00
Sindre Sorhus 21c1db5aa0 Minor TypeScript definition tweak 2019-06-07 14:44:51 +07:00
Corey Farrell bd197bb9c1 Add Node.js 12 to testing (#5) 2019-04-26 11:53:26 +07:00
Sindre Sorhus 267600324e 2.1.0 2019-04-03 12:21:09 +07:00
Dimitri Benin 23a0139209 Refactor TypeScript definition to CommonJS compatible export (#4) 2019-04-03 12:19:13 +07:00
11 changed files with 86 additions and 54 deletions

2
.github/funding.yml vendored Normal file
View File

@ -0,0 +1,2 @@
github: sindresorhus
tidelift: npm/p-defer

3
.github/security.md vendored Normal file
View File

@ -0,0 +1,3 @@
# Security Policy
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.

21
.github/workflows/main.yml vendored Normal file
View File

@ -0,0 +1,21 @@
name: CI
on:
- push
- pull_request
jobs:
test:
name: Node.js ${{ matrix.node-version }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
node-version:
- 14
- 12
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm test

View File

@ -1,5 +0,0 @@
language: node_js
node_js:
- '10'
- '8'
- '6'

26
index.d.ts vendored
View File

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

View File

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

View File

@ -1,5 +1,5 @@
import {expectType} from 'tsd-check';
import pDefer, {DeferredPromise} from '.';
import {expectType} from 'tsd';
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

@ -1,19 +1,21 @@
{
"name": "p-defer",
"version": "2.0.1",
"version": "4.0.0",
"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"
},
"exports": "./index.js",
"engines": {
"node": ">=6"
"node": ">=12"
},
"scripts": {
"test": "xo && ava && tsd-check"
"test": "xo && ava && tsd"
},
"files": [
"index.js",
@ -29,12 +31,11 @@
"later",
"async",
"await",
"promises",
"bluebird"
"promises"
],
"devDependencies": {
"ava": "^1.3.1",
"tsd-check": "^0.5.0",
"xo": "^0.24.0"
"ava": "^3.15.0",
"tsd": "^0.14.0",
"xo": "^0.38.2"
}
}

View File

@ -1,50 +1,51 @@
# p-defer [![Build Status](https://travis-ci.org/sindresorhus/p-defer.svg?branch=master)](https://travis-ci.org/sindresorhus/p-defer)
# p-defer
> Create a deferred promise
[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()`.
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)
---
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)
<div align="center">
<b>
<a href="https://tidelift.com/subscription/pkg/npm-p-defer?utm_source=npm-p-defer&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
</b>
<br>
<sub>
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
</sub>
</div>

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