Last active
September 24, 2019 06:21
-
-
Save odanado/f89122acb70b86ee8d0bc58e045d9355 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function taskA(i: number) { | |
return new Promise<number>((resolve, reject) => { | |
if (i % 2 == 0) reject(`error on taskA ${i}`) | |
resolve(i) | |
}) | |
} | |
function taskB(i: number) { | |
return new Promise<number>((resolve, reject) => { | |
if (i % 3 == 0) reject(`error on taskB ${i}`) | |
resolve(i) | |
}) | |
} | |
function taskC(i: number) { | |
return new Promise<number>((resolve, reject) => { | |
if (i % 5 == 0) reject(`error on taskC ${i}`) | |
resolve(i) | |
}) | |
} | |
function g(i: number) { | |
return new Promise((resolve, reject) => { | |
taskA(i) | |
.then(i => { | |
taskB(i) | |
.then(i => { | |
taskC(i) | |
.then(i => { | |
resolve(i) | |
}) | |
.catch(() => { | |
reject('error taskC') | |
}) | |
}) | |
.catch(() => { | |
reject('error taskB') | |
}) | |
}) | |
.catch(() => { | |
reject('error taskA') | |
}) | |
}) | |
} | |
function f(i: number) { | |
return taskA(i) | |
.then(i => i) | |
.catch(() => { | |
throw 'error taskA' | |
}) | |
.then(taskB) | |
.then(i => i) | |
.catch(() => { | |
throw 'error taskB' | |
}) | |
.then(taskC) | |
.then(i => i) | |
.catch(() => { | |
throw 'error taskC' | |
}) | |
} | |
async function test() { | |
for (let i = 1; i <= 30; i++) { | |
const resultF = await f(i).catch(e => e) | |
const resultG = await g(i).catch(e => e) | |
if (resultF === resultG) { | |
console.log('ok', i) | |
} | |
else { | |
console.log(resultF, resultG) | |
console.log('ng', i) | |
} | |
} | |
} | |
// g(3) | |
test() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
コールバック地獄な g を flat な f に置き換えて、同じエラーハンドリングをしたい
エラーハンドリング時にエラーメッセージを見るのは禁止という設定