Skip to content

Commit b9c8cf3

Browse files
authored
Update promise.md
1 parent 2694e13 commit b9c8cf3

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

docs/promise.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,39 @@ Promise.resolve(123)
294294
})
295295
```
296296

297+
* Only the relevant (nearest tailing) `catch` is called for a given error (as the catch starts a new promise chain).
298+
299+
```ts
300+
Promise.resolve(123)
301+
.then((res) => {
302+
throw new Error('something bad happened'); // throw a synchronous error
303+
return 456;
304+
})
305+
.catch((err) => {
306+
console.log('first catch: ' + err.message); // something bad happened
307+
return 123;
308+
})
309+
.then((res) => {
310+
console.log(res); // 123
311+
return Promise.resolve(789);
312+
})
313+
.catch((err) => {
314+
console.log('second catch: ' + err.message); // never called
315+
})
316+
```
317+
318+
* A `catch` is only called in case of an error in the preceeding chain:
319+
320+
```ts
321+
Promise.resolve(123)
322+
.then((res) => {
323+
return 456;
324+
})
325+
.catch((err) => {
326+
console.log("HERE"); // never called
327+
})
328+
```
329+
297330
The fact that:
298331

299332
* errors jump to the tailing `catch` (and skip any middle `then` calls) and

0 commit comments

Comments
 (0)