Skip to content

Instantly share code, notes, and snippets.

@odanado
Last active September 24, 2019 06:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save odanado/f89122acb70b86ee8d0bc58e045d9355 to your computer and use it in GitHub Desktop.
Save odanado/f89122acb70b86ee8d0bc58e045d9355 to your computer and use it in GitHub Desktop.
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()
@odanado
Copy link
Author

odanado commented Sep 24, 2019

コールバック地獄な g を flat な f に置き換えて、同じエラーハンドリングをしたい
エラーハンドリング時にエラーメッセージを見るのは禁止という設定

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment