This commit is contained in:
Rico Sta. Cruz 2016-12-27 01:08:47 +08:00
parent e631764d09
commit e77149233b
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 29 additions and 20 deletions

View File

@ -47,25 +47,34 @@ Promise.join(
``` ```
### Multiple promises (array) ### Multiple promises (array)
Use
[.all](http://bluebirdjs.com/docs/api/promise.all.html), - [Promise.all](http://bluebirdjs.com/docs/api/promise.all.html)([p]) - expect all to pass
[.any](http://bluebirdjs.com/docs/api/promise.any.html), - [Promise.some](http://bluebirdjs.com/docs/api/promise.some.html)([p], count) - expect `count` to pass
[.race](http://bluebirdjs.com/docs/api/promise.race.html), or - [Promise.any](http://bluebirdjs.com/docs/api/promise.any.html)([p]) - same as `some([p], 1)`
[.some](http://bluebirdjs.com/docs/api/promise.some.html). - [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 ```js
Promise.all([ promise1, promise2 ]) Promise.all([ promise1, promise2 ])
.then(function (results) { .then(results => {
results[0] results[0]
results[1] results[1]
}) })
// succeeds if one succeeds first // succeeds if one succeeds first
Promise.any(promises) 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 ### Object
Usually it's better to use `.join`, but whatever. Usually it's better to use `.join`, but whatever.
@ -74,7 +83,7 @@ Promise.props({
photos: get('photos'), photos: get('photos'),
posts: get('posts') posts: get('posts')
}) })
.then(function (res) { .then(res => {
res.photos res.photos
res.posts res.posts
}) })
@ -85,10 +94,10 @@ Use [Promise.try](http://bluebirdjs.com/docs/api/promise.try.html).
```js ```js
function getPhotos() { function getPhotos() {
return Promise.try(function () { return Promise.try(() => {
if (err) throw new Error("boo"); if (err) throw new Error("boo")
return result; return result
}); })
} }
getPhotos().then(...) getPhotos().then(...)
@ -98,19 +107,19 @@ getPhotos().then(...)
See [Promisification](http://bluebirdjs.com/docs/api/promisification.html). See [Promisification](http://bluebirdjs.com/docs/api/promisification.html).
```js ```js
var readFile = Promise.promisify(fs.readFile); var readFile = Promise.promisify(fs.readFile)
var fs = Promise.promisifyAll(require('fs')); var fs = Promise.promisifyAll(require('fs'))
``` ```
### Promise-returning methods ### Promise-returning methods
See [Promise.method](http://bluebirdjs.com/docs/api/promise.method.html). See [Promise.method](http://bluebirdjs.com/docs/api/promise.method.html).
```js ```js
User.login = Promise.method(function(email, password) { User.login = Promise.method((email, password) => {
if (!valid) 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) { User.login = Promise.coroutine(function* (email, password) {
let user = yield User.find({email: email}).fetch(); let user = yield User.find({email: email}).fetch()
return user; return user
}); })
``` ```
## Reference ## Reference