Compare commits
11 Commits
Author | SHA1 | Date |
---|---|---|
Derrick Hammer | 534ba472a8 | |
Sindre Sorhus | 0abf7f5633 | |
Sindre Sorhus | f83ba3bb30 | |
Richie Bendall | 857bca9cf5 | |
Sindre Sorhus | b59e39440b | |
Sindre Sorhus | 11f2980f79 | |
Sindre Sorhus | 71b64de6d6 | |
Sindre Sorhus | 21c1db5aa0 | |
Corey Farrell | bd197bb9c1 | |
Sindre Sorhus | 267600324e | |
Dimitri Benin | 23a0139209 |
|
@ -0,0 +1,2 @@
|
|||
github: sindresorhus
|
||||
tidelift: npm/p-defer
|
|
@ -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.
|
|
@ -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
|
|
@ -1,5 +0,0 @@
|
|||
language: node_js
|
||||
node_js:
|
||||
- '10'
|
||||
- '8'
|
||||
- '6'
|
|
@ -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>;
|
||||
|
|
19
index.js
19
index.js
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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>());
|
||||
|
|
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:
|
||||
|
||||
|
|
19
package.json
19
package.json
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
33
readme.md
33
readme.md
|
@ -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>
|
||||
|
|
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