File tree Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Expand file tree Collapse file tree 1 file changed +33
-0
lines changed Original file line number Diff line number Diff line change @@ -294,6 +294,39 @@ Promise.resolve(123)
294
294
})
295
295
```
296
296
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
+
297
330
The fact that:
298
331
299
332
* errors jump to the tailing ` catch ` (and skip any middle ` then ` calls) and
You can’t perform that action at this time.
0 commit comments