You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

47 lines
988 B

4 years ago
  1. # p-defer [![Build Status](https://travis-ci.org/sindresorhus/p-defer.svg?branch=master)](https://travis-ci.org/sindresorhus/p-defer)
  2. > Create a deferred promise
  3. [**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.
  4. ## Install
  5. ```
  6. $ npm install --save p-defer
  7. ```
  8. ## Usage
  9. ```js
  10. const pDefer = require('p-defer');
  11. function delay(ms) {
  12. const deferred = pDefer();
  13. setTimeout(deferred.resolve, ms, '🦄');
  14. return deferred.promise;
  15. }
  16. delay(100).then(console.log);
  17. //=> '🦄'
  18. ```
  19. *The above is just an example. Use [`delay`](https://github.com/sindresorhus/delay) if you need to delay a promise.*
  20. ## API
  21. ### pDefer()
  22. Returns an `Object` with a `promise` property and functions to `resolve()` and `reject()`.
  23. ## Related
  24. - [More…](https://github.com/sindresorhus/promise-fun)
  25. ## License
  26. MIT © [Sindre Sorhus](https://sindresorhus.com)