1 |
3a515b92
|
cagy
|
# p-defer [](https://travis-ci.org/sindresorhus/p-defer)
|
2 |
|
|
|
3 |
|
|
> Create a deferred promise
|
4 |
|
|
|
5 |
|
|
[**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.
|
6 |
|
|
|
7 |
|
|
|
8 |
|
|
## Install
|
9 |
|
|
|
10 |
|
|
```
|
11 |
|
|
$ npm install --save p-defer
|
12 |
|
|
```
|
13 |
|
|
|
14 |
|
|
|
15 |
|
|
## Usage
|
16 |
|
|
|
17 |
|
|
```js
|
18 |
|
|
const pDefer = require('p-defer');
|
19 |
|
|
|
20 |
|
|
function delay(ms) {
|
21 |
|
|
const deferred = pDefer();
|
22 |
|
|
setTimeout(deferred.resolve, ms, '🦄');
|
23 |
|
|
return deferred.promise;
|
24 |
|
|
}
|
25 |
|
|
|
26 |
|
|
delay(100).then(console.log);
|
27 |
|
|
//=> '🦄'
|
28 |
|
|
```
|
29 |
|
|
|
30 |
|
|
*The above is just an example. Use [`delay`](https://github.com/sindresorhus/delay) if you need to delay a promise.*
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
## API
|
34 |
|
|
|
35 |
|
|
### pDefer()
|
36 |
|
|
|
37 |
|
|
Returns an `Object` with a `promise` property and functions to `resolve()` and `reject()`.
|
38 |
|
|
|
39 |
|
|
|
40 |
|
|
## Related
|
41 |
|
|
|
42 |
|
|
- [More…](https://github.com/sindresorhus/promise-fun)
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
## License
|
46 |
|
|
|
47 |
|
|
MIT © [Sindre Sorhus](https://sindresorhus.com)
|