3 ms·
Parent's code with callbacks would be more like this: fetch((err, res) => { if (err) { // yay - error handling! :) } res.json((err, myJson
by art0rz 7y ago
Parent's code with callbacks would be more like this:
fetch((err, res) => {
if (err) {
// yay - error handling! :)
}
res.json((err, myJson) => {
if (err) {
// oh no - another error handler :(
}
// do stuff
});
}
fetch.json is async as well. With await the try / catch would just be around both await calls.
- gbuk2013 7y agoYes this is true, as long as your error handling is the same for all calls (not necessarily the case, especially if logging is involved). I don't actually have a problem with the callback version because it is nice and explicit. But again, I did originally say say that "async/await" was actually a valid use case in this necessarily serial scenario (and is exactly what I do in my article that I linked to). ;) That said, if I see a chain of serial calls more than a few levels deep it is a code smell that indicates refactoring is necessary. There really is no reason to write code like that unless it's very simple logic. This is why I have never had a problem with "callback hell" even though I write code that is heavily asynchronous (APIs, WebSocket, pub/sub etc.)