Last active
February 6, 2018 01:19
-
-
Save wesleytodd/fcf0a2b6ad0e1b576f436724c8eda915 to your computer and use it in GitHub Desktop.
Error handling with `async`/`await`
This file contains hidden or 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
const operations = { | |
op1: () => Promise.resolve([null, 'foo']), | |
op2: (res) => Promise.resolve([new Error(`Ugh, an error: ${res}`)]), | |
op3: (res) => Promise.resolve([null, 'op3 ' + res]) | |
}; | |
module.exports = async function operation () { | |
let [err1, res1] = await operations.op1(); | |
if (err1) { | |
console.error('error 1', err1); | |
return false; | |
} | |
// This is the case which is "less nice" for sure | |
let [[err2, res2], [err3, res3]] = await Promise.all([ | |
operations.op2(res1), | |
operations.op3(res1) | |
]); | |
// Or these could be broken up into multiple ifs | |
if (err2 || err3) { | |
err2 && console.error('error 2', err2); | |
err3 && console.error('error 3', err3); | |
return false; | |
} | |
return [res2, res3]; | |
}; |
This file contains hidden or 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
const operations = { | |
op1: () => Promise.resolve('foo'), | |
op2: (res) => Promise.reject(new Error(`Ugh, an error: ${res}`)), | |
op3: (res) => Promise.resolve('op3 ' + res) | |
}; | |
module.exports = async function operation () { | |
let [err1, res1] = await customPromisify(operations.op1)(); | |
if (err1) { | |
console.error('error 1', err1); | |
return false; | |
} | |
let [[err2, res2], [err3, res3]] = await Promise.all([ | |
customPromisify(operations.op2)(res1), | |
customPromisify(operations.op3)(res1) | |
]); | |
// Or these could be broken up into multiple ifs | |
if (err2 || err3) { | |
err2 && console.error('error 2', err2); | |
err3 && console.error('error 3', err3); | |
return false; | |
} | |
return [res2, res3]; | |
}; | |
function customPromisify (fnc) { | |
return async function (...args) { | |
try { | |
return [null, await fnc(...args)]; | |
} catch (e) { | |
return [e]; | |
} | |
} | |
} |
This file contains hidden or 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
[ | |
require('./try-catch'), | |
require('./error-tuple'), | |
require('./multi-catch'), | |
require('./hybrid') | |
].forEach(async function (f) { | |
if (await f() !== false) { | |
console.error(new Error('wrong, shouldnt be hit')); | |
} | |
}); |
This file contains hidden or 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
const operations = { | |
op1: () => Promise.resolve('foo'), | |
op2: (res) => Promise.reject(new Error(`Ugh, an error: ${res}`)), | |
op3: (res) => Promise.resolve('op3 ' + res) | |
}; | |
module.exports = async function operation () { | |
try { | |
let res1 = await operations.op1().catch((e) => console.error('error 1', e)); | |
return await Promise.all([ | |
operations.op2(res1).catch((e) => { console.error('error 2', e); throw e; }), | |
operations.op3(res1).catch((e) => { console.error('error 3', e); throw e; }) | |
]); | |
} catch (e) { | |
return false; | |
} | |
}; |
This file contains hidden or 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
const operations = { | |
op1: () => Promise.resolve('foo'), | |
op2: (res) => Promise.reject(new Error(`Ugh, an error: ${res}`)), | |
op3: (res) => Promise.resolve('op3 ' + res) | |
}; | |
module.exports = async function operation () { | |
let res1; | |
try { | |
res1 = await operations.op1(); | |
} catch (err1) { | |
console.error('error 1', err1); | |
return false; | |
} | |
try { | |
return await Promise.all([operations.op2(res1), operations.op3(res1)]); | |
} catch(e) { | |
// Here you dont know if it was op2 or op3 which errored. | |
// Also, if both error, you only get and log the first | |
console.error('error ?', e); | |
return false; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment