Update
This commit is contained in:
parent
e631764d09
commit
e77149233b
49
bluebird.md
49
bluebird.md
|
@ -47,25 +47,34 @@ Promise.join(
|
|||
```
|
||||
|
||||
### Multiple promises (array)
|
||||
Use
|
||||
[.all](http://bluebirdjs.com/docs/api/promise.all.html),
|
||||
[.any](http://bluebirdjs.com/docs/api/promise.any.html),
|
||||
[.race](http://bluebirdjs.com/docs/api/promise.race.html), or
|
||||
[.some](http://bluebirdjs.com/docs/api/promise.some.html).
|
||||
|
||||
- [Promise.all](http://bluebirdjs.com/docs/api/promise.all.html)([p]) - expect all to pass
|
||||
- [Promise.some](http://bluebirdjs.com/docs/api/promise.some.html)([p], count) - expect `count` to pass
|
||||
- [Promise.any](http://bluebirdjs.com/docs/api/promise.any.html)([p]) - same as `some([p], 1)`
|
||||
- [Promise.race](http://bluebirdjs.com/docs/api/promise.race.html)([p], count) - use `.any` instead
|
||||
- [Promise.map](http://bluebirdjs.com/docs/api/promise.map.html)([p], fn, options) - supports concurrency
|
||||
|
||||
```js
|
||||
Promise.all([ promise1, promise2 ])
|
||||
.then(function (results) {
|
||||
.then(results => {
|
||||
results[0]
|
||||
results[1]
|
||||
})
|
||||
|
||||
// succeeds if one succeeds first
|
||||
Promise.any(promises)
|
||||
.then(function (result) {
|
||||
.then(results => {
|
||||
})
|
||||
```
|
||||
|
||||
Use [Promise.map](http://bluebirdjs.com/docs/api/promise.map.html) to "promisify" a list of values.
|
||||
|
||||
```js
|
||||
Promise.map(urls, url => fetch(url))
|
||||
.then(...)
|
||||
```
|
||||
|
||||
|
||||
### Object
|
||||
Usually it's better to use `.join`, but whatever.
|
||||
|
||||
|
@ -74,7 +83,7 @@ Promise.props({
|
|||
photos: get('photos'),
|
||||
posts: get('posts')
|
||||
})
|
||||
.then(function (res) {
|
||||
.then(res => {
|
||||
res.photos
|
||||
res.posts
|
||||
})
|
||||
|
@ -85,10 +94,10 @@ Use [Promise.try](http://bluebirdjs.com/docs/api/promise.try.html).
|
|||
|
||||
```js
|
||||
function getPhotos() {
|
||||
return Promise.try(function () {
|
||||
if (err) throw new Error("boo");
|
||||
return result;
|
||||
});
|
||||
return Promise.try(() => {
|
||||
if (err) throw new Error("boo")
|
||||
return result
|
||||
})
|
||||
}
|
||||
|
||||
getPhotos().then(...)
|
||||
|
@ -98,19 +107,19 @@ getPhotos().then(...)
|
|||
See [Promisification](http://bluebirdjs.com/docs/api/promisification.html).
|
||||
|
||||
```js
|
||||
var readFile = Promise.promisify(fs.readFile);
|
||||
var fs = Promise.promisifyAll(require('fs'));
|
||||
var readFile = Promise.promisify(fs.readFile)
|
||||
var fs = Promise.promisifyAll(require('fs'))
|
||||
```
|
||||
|
||||
### Promise-returning methods
|
||||
See [Promise.method](http://bluebirdjs.com/docs/api/promise.method.html).
|
||||
|
||||
```js
|
||||
User.login = Promise.method(function(email, password) {
|
||||
User.login = Promise.method((email, password) => {
|
||||
if (!valid)
|
||||
throw new Error("Email not valid");
|
||||
throw new Error("Email not valid")
|
||||
|
||||
return /* promise */;
|
||||
return /* promise */
|
||||
});
|
||||
```
|
||||
|
||||
|
@ -119,9 +128,9 @@ See [Promise.coroutine](http://bluebirdjs.com/docs/api/promise.coroutine.html).
|
|||
|
||||
```
|
||||
User.login = Promise.coroutine(function* (email, password) {
|
||||
let user = yield User.find({email: email}).fetch();
|
||||
return user;
|
||||
});
|
||||
let user = yield User.find({email: email}).fetch()
|
||||
return user
|
||||
})
|
||||
```
|
||||
|
||||
## Reference
|
||||
|
|
Loading…
Reference in New Issue