You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+30-27Lines changed: 30 additions & 27 deletions
Original file line number
Diff line number
Diff line change
@@ -176,16 +176,33 @@ loadImageAsync('one.png')
176
176
177
177
`then` takes two functions, either of which can be `null` or undefined. The `resolved` callback is called when the promise succeeds, and it is passed the resolved value (in this case `image`). The `rejected` callback is called when the promise fails, and it is passed the `Error` object we created earlier.
178
178
179
+
### `.catch(err)`
180
+
181
+
Promises also have a `.catch(func)` to handle errors, which is the same as `.then(null, func)` but provides clearer intent.
182
+
183
+
```js
184
+
loadImageAsync('one.png')
185
+
.catch(function(err) {
186
+
console.error('Could not load image', err);
187
+
});
188
+
```
189
+
179
190
### chaining
180
191
181
-
The `.then()` method *always returns a Promise*, which means it can be chained. The above could be re-written like so, where the second callback handles any errors in the promise before it:
192
+
The `.then()` method *always returns a Promise*, which means it can be chained. The above could be re-written like so. If a promise is rejected, the next `catch()` or `then(null, rejected)` will be called.
193
+
194
+
In the following example, if the `loadImageAsync` method is rejected, the only output to the console will be the error message.
@@ -194,18 +211,16 @@ In general, you should be wary of long promise chains. They can be difficult to
194
211
195
212
### resolving values
196
213
197
-
Your callbacks can return a value to pass it along to the next `.then()`method. For example:
214
+
Your `then()` and `catch()`callbacks can return a value to pass it along to the next method in the chain. For example, here we resolve errors to a default image:
198
215
199
216
```js
200
217
loadImageAsync('one.png')
201
-
.then(null, function(err) {
218
+
.catch(function(err) {
202
219
console.warn(err.message);
203
220
return notFoundImage;
204
221
})
205
222
.then(function(image) {
206
223
console.log('Resolved image', image);
207
-
}, function(err) {
208
-
console.error('Could not find any images', err);
209
224
});
210
225
```
211
226
@@ -215,33 +230,20 @@ The cool thing is, you can return a `Promise` instance, and it will be resolved
215
230
216
231
```js
217
232
loadImageAsync('one.png')
218
-
.then(null, function(err) {
233
+
.catch(function(err) {
219
234
console.warn(err.message);
220
235
returnloadImageAsync('not-found.png');
221
236
})
222
237
.then(function(image) {
223
238
console.log('Resolved image', image);
224
-
}, function(err) {
225
-
console.error('Could not find any images', err);
226
-
});
227
-
```
228
-
229
-
The above tries to load `'one.png'`, but if that fails it will then load `'not-found.png'`, and only then would it resolve to the image.
230
-
231
-
### `.catch(err)`
232
-
233
-
You can also use `.catch(func)` to handle errors, which is the same as using `.then(null, func)`.
234
-
235
-
```js
236
-
loadImageAsync('one.png')
237
-
.then(function(image) {
238
-
console.log('Loaded image', image);
239
-
});
239
+
})
240
240
.catch(function(err) {
241
-
console.error('Could not load image', err);
241
+
console.error('Could not load any images', err);
242
242
});
243
243
```
244
244
245
+
The above tries to load `'one.png'`, but if that fails it will then load `'not-found.png'`.
246
+
245
247
### `Promise.all()`
246
248
247
249
Let's go back to our original task of loading multiple images.
@@ -288,7 +290,8 @@ function getUserImages(user) {
288
290
289
291
functionshowUserImages(user) {
290
292
returngetUserImages(user)
291
-
.then(renderGallery, renderEmptyGallery);
293
+
.then(renderGallery)
294
+
.catch(renderEmptyGallery);
292
295
}
293
296
294
297
showUserImages('mattdesl')
@@ -305,13 +308,13 @@ In the following example, if the user has not activated their account, the promi
305
308
306
309
```js
307
310
loadUser()
308
-
.then(function(user) {
311
+
.then(function(user) {
309
312
if (!user.activated) {
310
313
thrownewError('user has not activated their account');
0 commit comments