This commit is contained in:
Sindre Sorhus 2016-10-21 12:07:52 +07:00
commit 5c579ad9e8
9 changed files with 153 additions and 0 deletions

12
.editorconfig Normal file
View File

@ -0,0 +1,12 @@
root = true
[*]
indent_style = tab
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[{package.json,*.yml}]
indent_style = space
indent_size = 2

2
.gitattributes vendored Normal file
View File

@ -0,0 +1,2 @@
* text=auto
*.js text eol=lf

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules

4
.travis.yml Normal file
View File

@ -0,0 +1,4 @@
language: node_js
node_js:
- '6'
- '4'

11
index.js Normal file
View File

@ -0,0 +1,11 @@
'use strict';
module.exports = () => {
const ret = {};
ret.promise = new Promise((resolve, reject) => {
ret.resolve = resolve;
ret.reject = reject;
});
return ret;
};

21
license Normal file
View File

@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (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:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

41
package.json Normal file
View File

@ -0,0 +1,41 @@
{
"name": "p-defer",
"version": "0.0.0",
"description": "Create a deferred promise",
"license": "MIT",
"repository": "sindresorhus/p-defer",
"author": {
"name": "Sindre Sorhus",
"email": "sindresorhus@gmail.com",
"url": "sindresorhus.com"
},
"engines": {
"node": ">=4"
},
"scripts": {
"test": "xo && ava"
},
"files": [
"index.js"
],
"keywords": [
"promise",
"defer",
"deferred",
"resolve",
"reject",
"lazy",
"later",
"async",
"await",
"promises",
"bluebird"
],
"devDependencies": {
"ava": "*",
"xo": "*"
},
"xo": {
"esnext": true
}
}

47
readme.md Normal file
View File

@ -0,0 +1,47 @@
# p-defer [![Build Status](https://travis-ci.org/sindresorhus/p-defer.svg?branch=master)](https://travis-ci.org/sindresorhus/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 --save p-defer
```
## Usage
```js
const pDefer = require('p-defer');
function delay(ms) {
const deferred = pDefer();
setTimeout(deferred.resolve, ms, '🦄');
return deferred.promise;
}
delay(100).then(console.log);
//=> '🦄'
```
*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
- [More…](https://github.com/sindresorhus/promise-fun)
## License
MIT © [Sindre Sorhus](https://sindresorhus.com)

14
test.js Normal file
View File

@ -0,0 +1,14 @@
import test from 'ava';
import m from './';
const fixture = Symbol('fixture');
function delay(ms) {
const deferred = m();
setTimeout(deferred.resolve, ms, fixture);
return deferred.promise;
}
test(async t => {
t.is(await delay(50), fixture);
});