bluebird: update

This commit is contained in:
Rico Sta. Cruz 2017-09-04 10:54:41 +08:00
parent 2a02808586
commit 397b5e32ec
No known key found for this signature in database
GPG Key ID: CAAD38AE2962619A
1 changed files with 44 additions and 25 deletions

View File

@ -1,39 +1,45 @@
--- ---
title: bluebird.js title: bluebird.js
category: JavaScript libraries category: JavaScript libraries
layout: default-ad layout: 2017/sheet
weight: -1
updated: 2017-09-04
--- ---
### Also see
Also see the [promise cheatsheet](promise.html) and [Bluebird.js API](https://github.com/petkaantonov/bluebird/blob/master/API.md) (github.com). Also see the [promise cheatsheet](promise.html) and [Bluebird.js API](https://github.com/petkaantonov/bluebird/blob/master/API.md) (github.com).
{:.center.brief-intro}
### Example
```js ```js
promise promise
.then(okFn, errFn) .then(okFn, errFn)
.spread(okFn, errFn) //* .spread(okFn, errFn) // *
.catch(errFn) .catch(errFn)
.catch(TypeError, errFn) //* .catch(TypeError, errFn) // *
.finally(fn) //* .finally(fn) // *
.map(function (e) { ... }) .map(function (e) { ··· }) // *
.each(function (e) { ... }) .each(function (e) { ··· }) // *
``` ```
---- Those marked with `*` are non-standard Promise API that only work with Bluebird promises.
### Multiple return values ### Multiple return values
Use [Promise.spread](http://bluebirdjs.com/docs/api/promise.spread.html).
```js ```js
.then(function () { .then(function () {
return [ 'abc', 'def' ]; return [ 'abc', 'def' ]
}) })
.spread(function (abc, def) { .spread(function (abc, def) {
... ···
}); })
``` ```
{: data-line="4"}
Use [Promise.spread](http://bluebirdjs.com/docs/api/promise.spread.html)
### Multiple promises ### Multiple promises
Use [Promise.join](http://bluebirdjs.com/docs/api/promise.join.html).
```js ```js
Promise.join( Promise.join(
@ -41,10 +47,13 @@ Promise.join(
getMessages(), getMessages(),
getTweets(), getTweets(),
function (pics, msgs, tweets) { function (pics, msgs, tweets) {
return ...; return ···
} }
) )
``` ```
{: data-line="1"}
Use [Promise.join](http://bluebirdjs.com/docs/api/promise.join.html)
### Multiple promises (array) ### Multiple promises (array)
@ -66,17 +75,17 @@ Promise.any(promises)
.then(results => { .then(results => {
}) })
``` ```
{: data-line="1,8"}
Use [Promise.map](http://bluebirdjs.com/docs/api/promise.map.html) to "promisify" a list of values.
```js ```js
Promise.map(urls, url => fetch(url)) Promise.map(urls, url => fetch(url))
.then(...) .then(···)
``` ```
{: data-line="1"}
Use [Promise.map](http://bluebirdjs.com/docs/api/promise.map.html) to "promisify" a list of values.
### Object ### Object
Usually it's better to use `.join`, but whatever.
```js ```js
Promise.props({ Promise.props({
@ -88,9 +97,11 @@ Promise.props({
res.posts res.posts
}) })
``` ```
{: data-line="1"}
Usually it's better to use `.join`, but whatever.
### Chain of promises ### Chain of promises
Use [Promise.try](http://bluebirdjs.com/docs/api/promise.try.html).
```js ```js
function getPhotos() { function getPhotos() {
@ -100,19 +111,23 @@ function getPhotos() {
}) })
} }
getPhotos().then(...) getPhotos().then(···)
``` ```
{: data-line="2"}
### Using Node-style functions Use [Promise.try](http://bluebirdjs.com/docs/api/promise.try.html).
See [Promisification](http://bluebirdjs.com/docs/api/promisification.html).
### Node-style functions
```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'))
``` ```
{: data-line="2"}
See [Promisification](http://bluebirdjs.com/docs/api/promisification.html).
### Promise-returning methods ### Promise-returning methods
See [Promise.method](http://bluebirdjs.com/docs/api/promise.method.html).
```js ```js
User.login = Promise.method((email, password) => { User.login = Promise.method((email, password) => {
@ -120,11 +135,13 @@ User.login = Promise.method((email, password) => {
throw new Error("Email not valid") throw new Error("Email not valid")
return /* promise */ return /* promise */
}); })
``` ```
{: data-line="1"}
See [Promise.method](http://bluebirdjs.com/docs/api/promise.method.html).
### Generators ### Generators
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) {
@ -133,6 +150,8 @@ User.login = Promise.coroutine(function* (email, password) {
}) })
``` ```
See [Promise.coroutine](http://bluebirdjs.com/docs/api/promise.coroutine.html).
## Reference ## Reference
<http://bluebirdjs.com/docs/api-reference.html> <http://bluebirdjs.com/docs/api-reference.html>