お、これこれ。.thenのチェーンの中でrejectしたら最後の.catchで拾って欲しい気がするんだけどそうなってない。
$ cat index.js
new Promise((resolve, reject) => {
resolve("最初の結果わよ")
}).then(
result => {
console.log("first result: " + result)
new Promise((resolve, reject) => {
reject("2番目のエラーわよ")
})
}
).then(
result => console.log("second result: " + result)
).catch(
err => console.error("error: " + err)
)
$ node index.js
first result: 最初の結果わよ
second result: undefined
(node:57758) UnhandledPromiseRejectionWarning: 2番目のエラーわよ
わかったー!.thenからPromiseを返さないといけないんだ。
$ cat index.js
new Promise((resolve, reject) => {
resolve("最初の結果わよ")
}).then(
result => {
console.log("first result: " + result)
return new Promise((resolve, reject) => {
reject("2番目のエラーわよ")
})
}
).then(
result => console.log("second result: " + result)
).catch(
err => console.error("error: " + err)
)
$ node index.js
first result: 最初の結果わよ
error: 2番目のエラーわよ