Promise then, catchの連結
以下のコードは、どのような出力になるか?
const promise = Promise.reject() promise .then(() => console.log(1)) .catch(() => console.log(2)) .then(() => console.log(3)) .catch(() => console.log(4)) .then(() => console.log(5)) .catch(() => console.log(6))
答えは「2, 3, 5」
初めの変数promiseは、rejectされたPromiseオブジェクト。
1のthenは、rejectされたPromiseオブジェクトだから通らない。
2のcatchは、rejectされたPromiseオブジェクトだから通る。
同時にreturnされたと見なされ、resolveされたPromiseオブジェクトが返却される。
3のthenは、resolveされたPromiseオブジェクトだから通る。
同時にreturnされたと見なされ、resolveされたPromiseオブジェクトが返却される。
4のcatchは、resolveされたPromiseオブジェクトだから通らない。
5のthenは、resolveされたPromiseオブジェクトだから通る。
同時にreturnされたと見なされ、resolveされたPromiseオブジェクトが返却される。
6のcatchは、resolveされたPromiseオブジェクトだから通らない。