cheatsheets/bluebird.md

158 lines
3.2 KiB
Markdown
Raw Permalink Normal View History

2014-09-24 09:09:41 +00:00
---
title: bluebird.js
2015-11-24 05:02:17 +00:00
category: JavaScript libraries
2017-09-04 02:54:41 +00:00
weight: -1
2020-07-04 13:33:09 +00:00
updated: 2017-09-08
2014-09-24 09:09:41 +00:00
---
2017-09-04 02:54:41 +00:00
### Also see
2015-04-17 19:17:51 +00:00
Also see the [promise cheatsheet](promise.html) and [Bluebird.js API](https://github.com/petkaantonov/bluebird/blob/master/API.md) (github.com).
2017-09-04 02:54:41 +00:00
### Example
2014-09-24 09:09:41 +00:00
```js
promise
.then(okFn, errFn)
2017-09-04 02:54:41 +00:00
.spread(okFn, errFn) // *
2014-09-24 09:09:41 +00:00
.catch(errFn)
2017-09-04 02:54:41 +00:00
.catch(TypeError, errFn) // *
.finally(fn)
2017-09-04 02:54:41 +00:00
.map(function (e) { ··· }) // *
.each(function (e) { ··· }) // *
2014-09-24 09:09:41 +00:00
```
2017-09-04 02:54:41 +00:00
Those marked with `*` are non-standard Promise API that only work with Bluebird promises.
2015-04-17 19:17:51 +00:00
### Multiple return values
2014-09-24 09:09:41 +00:00
```js
2015-04-17 19:17:51 +00:00
.then(function () {
2017-09-04 02:54:41 +00:00
return [ 'abc', 'def' ]
2015-04-17 19:17:51 +00:00
})
.spread(function (abc, def) {
2017-09-04 02:54:41 +00:00
···
})
2015-04-17 19:17:51 +00:00
```
2017-09-04 02:54:41 +00:00
{: data-line="4"}
Use [Promise.spread](http://bluebirdjs.com/docs/api/promise.spread.html)
2015-04-17 19:17:51 +00:00
### Multiple promises
```js
Promise.join(
getPictures(),
getMessages(),
getTweets(),
function (pics, msgs, tweets) {
2017-09-04 02:54:41 +00:00
return ···
2015-04-17 19:17:51 +00:00
}
)
```
2017-09-04 02:54:41 +00:00
{: data-line="1"}
Use [Promise.join](http://bluebirdjs.com/docs/api/promise.join.html)
2015-04-17 19:17:51 +00:00
### Multiple promises (array)
2016-12-26 17:08:47 +00:00
- [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
2015-04-17 19:17:51 +00:00
```js
2015-04-17 19:35:46 +00:00
Promise.all([ promise1, promise2 ])
2016-12-26 17:08:47 +00:00
.then(results => {
2015-04-17 19:17:51 +00:00
results[0]
results[1]
})
// succeeds if one succeeds first
Promise.any(promises)
2016-12-26 17:08:47 +00:00
.then(results => {
2015-04-17 19:35:46 +00:00
})
2014-09-24 09:09:41 +00:00
```
2017-09-04 02:54:41 +00:00
{: data-line="1,8"}
2016-12-26 17:08:47 +00:00
```js
Promise.map(urls, url => fetch(url))
2017-09-04 02:54:41 +00:00
.then(···)
2016-12-26 17:08:47 +00:00
```
2017-09-04 02:54:41 +00:00
{: data-line="1"}
2016-12-26 17:08:47 +00:00
2017-09-04 02:54:41 +00:00
Use [Promise.map](http://bluebirdjs.com/docs/api/promise.map.html) to "promisify" a list of values.
2016-12-26 17:08:47 +00:00
2015-04-17 19:17:51 +00:00
### Object
2014-09-24 09:09:41 +00:00
```js
Promise.props({
photos: get('photos'),
posts: get('posts')
})
2016-12-26 17:08:47 +00:00
.then(res => {
2014-09-24 09:09:41 +00:00
res.photos
res.posts
})
```
2017-09-04 02:54:41 +00:00
{: data-line="1"}
2017-09-07 23:23:19 +00:00
Use [Promise.props](http://bluebirdjs.com/docs/api/promise.props.html).
2014-09-24 09:09:41 +00:00
### Chain of promises
```js
2015-04-17 19:01:27 +00:00
function getPhotos() {
2016-12-26 17:08:47 +00:00
return Promise.try(() => {
if (err) throw new Error("boo")
return result
})
2015-04-17 19:01:27 +00:00
}
2017-09-04 02:54:41 +00:00
getPhotos().then(···)
2015-04-17 18:58:04 +00:00
```
2017-09-04 02:54:41 +00:00
{: data-line="2"}
2014-09-24 09:09:41 +00:00
2017-09-04 02:54:41 +00:00
Use [Promise.try](http://bluebirdjs.com/docs/api/promise.try.html).
### Node-style functions
2014-09-24 09:09:41 +00:00
```js
2016-12-26 17:08:47 +00:00
var readFile = Promise.promisify(fs.readFile)
var fs = Promise.promisifyAll(require('fs'))
2014-09-24 09:09:41 +00:00
```
2017-09-04 02:54:41 +00:00
{: data-line="2"}
See [Promisification](http://bluebirdjs.com/docs/api/promisification.html).
2015-06-07 05:42:45 +00:00
### Promise-returning methods
```js
2016-12-26 17:08:47 +00:00
User.login = Promise.method((email, password) => {
2015-06-07 05:42:45 +00:00
if (!valid)
2016-12-26 17:08:47 +00:00
throw new Error("Email not valid")
2015-06-07 05:42:45 +00:00
2016-12-26 17:08:47 +00:00
return /* promise */
2017-09-04 02:54:41 +00:00
})
2015-06-07 05:42:45 +00:00
```
2017-09-04 02:54:41 +00:00
{: data-line="1"}
See [Promise.method](http://bluebirdjs.com/docs/api/promise.method.html).
2015-06-07 05:42:45 +00:00
### Generators
2017-09-07 23:23:19 +00:00
```js
2015-06-07 05:42:45 +00:00
User.login = Promise.coroutine(function* (email, password) {
2016-12-26 17:08:47 +00:00
let user = yield User.find({email: email}).fetch()
return user
})
2015-06-07 05:42:45 +00:00
```
2017-09-07 23:23:19 +00:00
{: data-line="1"}
2016-12-26 17:01:36 +00:00
2017-09-04 02:54:41 +00:00
See [Promise.coroutine](http://bluebirdjs.com/docs/api/promise.coroutine.html).
2016-12-26 17:01:36 +00:00
## Reference
<http://bluebirdjs.com/docs/api-reference.html>