Skip to content

Commit f785d0c

Browse files
committed
use catch() for clearer intent
1 parent de8103a commit f785d0c

File tree

1 file changed

+30
-27
lines changed

1 file changed

+30
-27
lines changed

README.md

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -176,16 +176,33 @@ loadImageAsync('one.png')
176176

177177
`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.
178178

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+
179190
### chaining
180191

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.
182195

183196
```js
184197
loadImageAsync('one.png')
185198
.then(function(image) {
186199
console.log('Image loaded', image);
200+
return { width: image.width, height: image.height };
201+
})
202+
.then(function(size) {
203+
console.log('Image size:', size);
187204
})
188-
.then(null, function(err) {
205+
.catch(function(err) {
189206
console.error('Error in promise chain', err);
190207
});
191208
```
@@ -194,18 +211,16 @@ In general, you should be wary of long promise chains. They can be difficult to
194211

195212
### resolving values
196213

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:
198215

199216
```js
200217
loadImageAsync('one.png')
201-
.then(null, function(err) {
218+
.catch(function(err) {
202219
console.warn(err.message);
203220
return notFoundImage;
204221
})
205222
.then(function(image) {
206223
console.log('Resolved image', image);
207-
}, function(err) {
208-
console.error('Could not find any images', err);
209224
});
210225
```
211226

@@ -215,33 +230,20 @@ The cool thing is, you can return a `Promise` instance, and it will be resolved
215230

216231
```js
217232
loadImageAsync('one.png')
218-
.then(null, function(err) {
233+
.catch(function(err) {
219234
console.warn(err.message);
220235
return loadImageAsync('not-found.png');
221236
})
222237
.then(function(image) {
223238
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+
})
240240
.catch(function(err) {
241-
console.error('Could not load image', err);
241+
console.error('Could not load any images', err);
242242
});
243243
```
244244

245+
The above tries to load `'one.png'`, but if that fails it will then load `'not-found.png'`.
246+
245247
### `Promise.all()`
246248

247249
Let's go back to our original task of loading multiple images.
@@ -288,7 +290,8 @@ function getUserImages(user) {
288290

289291
function showUserImages(user) {
290292
return getUserImages(user)
291-
.then(renderGallery, renderEmptyGallery);
293+
.then(renderGallery)
294+
.catch(renderEmptyGallery);
292295
}
293296

294297
showUserImages('mattdesl')
@@ -305,13 +308,13 @@ In the following example, if the user has not activated their account, the promi
305308

306309
```js
307310
loadUser()
308-
.then(function (user) {
311+
.then(function(user) {
309312
if (!user.activated) {
310313
throw new Error('user has not activated their account');
311314
}
312315
return showUserGallery(user);
313316
})
314-
.then(null, function (err) {
317+
.catch(function(err) {
315318
showError(err.message);
316319
});
317320
```

0 commit comments

Comments
 (0)