OSDN Git Service

Regular updates
[twpd/master.git] / bluebird.md
1 ---
2 title: bluebird.js
3 category: JavaScript libraries
4 layout: 2017/sheet
5 weight: -1
6 updated: 2017-09-08
7 ---
8
9 ### Also see
10
11 Also see the [promise cheatsheet](promise.html) and [Bluebird.js API](https://github.com/petkaantonov/bluebird/blob/master/API.md) (github.com).
12
13 ### Example
14
15 ```js
16 promise
17   .then(okFn, errFn)
18   .spread(okFn, errFn)        // *
19   .catch(errFn)
20   .catch(TypeError, errFn)    // *
21   .finally(fn)
22   .map(function (e) { ··· })  // *
23   .each(function (e) { ··· }) // *
24 ```
25
26 Those marked with `*` are non-standard Promise API that only work with Bluebird promises.
27
28 ### Multiple return values
29
30 ```js
31 .then(function () {
32   return [ 'abc', 'def' ]
33 })
34 .spread(function (abc, def) {
35   ···
36 })
37 ```
38 {: data-line="4"}
39
40 Use [Promise.spread](http://bluebirdjs.com/docs/api/promise.spread.html)
41
42 ### Multiple promises
43
44 ```js
45 Promise.join(
46   getPictures(),
47   getMessages(),
48   getTweets(),
49   function (pics, msgs, tweets) {
50     return ···
51   }
52 )
53 ```
54 {: data-line="1"}
55
56 Use [Promise.join](http://bluebirdjs.com/docs/api/promise.join.html)
57
58 ### Multiple promises (array)
59
60 - [Promise.all](http://bluebirdjs.com/docs/api/promise.all.html)([p]) - expect all to pass
61 - [Promise.some](http://bluebirdjs.com/docs/api/promise.some.html)([p], count) - expect `count` to pass
62 - [Promise.any](http://bluebirdjs.com/docs/api/promise.any.html)([p]) - same as `some([p], 1)`
63 - [Promise.race](http://bluebirdjs.com/docs/api/promise.race.html)([p], count) - use `.any` instead
64 - [Promise.map](http://bluebirdjs.com/docs/api/promise.map.html)([p], fn, options) - supports concurrency
65
66 ```js
67 Promise.all([ promise1, promise2 ])
68   .then(results => {
69     results[0]
70     results[1]
71   })
72
73 // succeeds if one succeeds first
74 Promise.any(promises)
75   .then(results => {
76   })
77 ```
78 {: data-line="1,8"}
79
80 ```js
81 Promise.map(urls, url => fetch(url))
82   .then(···)
83 ```
84 {: data-line="1"}
85
86 Use [Promise.map](http://bluebirdjs.com/docs/api/promise.map.html) to "promisify" a list of values.
87
88 ### Object
89
90 ```js
91 Promise.props({
92   photos: get('photos'),
93   posts: get('posts')
94 })
95 .then(res => {
96   res.photos
97   res.posts
98 })
99 ```
100 {: data-line="1"}
101
102 Use [Promise.props](http://bluebirdjs.com/docs/api/promise.props.html).
103
104 ### Chain of promises
105
106 ```js
107 function getPhotos() {
108   return Promise.try(() => {
109     if (err) throw new Error("boo")
110     return result
111   })
112 }
113
114 getPhotos().then(···)
115 ```
116 {: data-line="2"}
117
118 Use [Promise.try](http://bluebirdjs.com/docs/api/promise.try.html).
119
120 ### Node-style functions
121
122 ```js
123 var readFile = Promise.promisify(fs.readFile)
124 var fs = Promise.promisifyAll(require('fs'))
125 ```
126 {: data-line="2"}
127
128 See [Promisification](http://bluebirdjs.com/docs/api/promisification.html).
129
130 ### Promise-returning methods
131
132 ```js
133 User.login = Promise.method((email, password) => {
134   if (!valid)
135     throw new Error("Email not valid")
136
137   return /* promise */
138 })
139 ```
140 {: data-line="1"}
141
142 See [Promise.method](http://bluebirdjs.com/docs/api/promise.method.html).
143
144 ### Generators
145
146 ```js
147 User.login = Promise.coroutine(function* (email, password) {
148   let user = yield User.find({email: email}).fetch()
149   return user
150 })
151 ```
152 {: data-line="1"}
153
154 See [Promise.coroutine](http://bluebirdjs.com/docs/api/promise.coroutine.html).
155
156 ## Reference
157
158 <http://bluebirdjs.com/docs/api-reference.html>