async function foo () {
throw new Error ('test')
}
async function bar () {
return foo()
}
async function baz () {
try {
await bar()
} catch (err) {
console.error(err.message)
}
}
That's not the case in callback-based systems where it's more of a golang style "handle it right then, or it gets ignored forever" kind of thing.But also unhandled promise rejection handlers are seeing much more widespread usage now to catch "unhandled promise rejections", which make it a bit easier to handle errors in async contexts, but i'll be the first to agree that there is still a lot that sucks about handling errors in async contexts in javascript...
> It also makes it harder to do more advanced designs like rate limiting, loading bars, etc.
It does, but you can also handle a lot of these by mixing promises and async/await (since a/a is basically a nicer syntax for Promises). And callbacks still exist for the cases that really need them (like you said, loading callbacks are still a hell of a lot easier to use than janky promise-based solutions in most cases in my opinion! Mostly because a promise is one-and-done, but callbacks can be re-called multiple times).
But the point is that if you stick to ONLY callbacks, EVERYTHING has to handle wrapping it. Whereas if you move on to using the new features where they make sense (the "easy path" can use async/await or async iterator streams), then only people who need those extra complicated features have to wrap and work with those more complicated setups.
If you return a promise you don’t really need to support a callback because you can just call .then on the promise with a callback. i.e.
func().then(result => {
// success
}, err => {
/ error
})
Is equivalent to func((err, result) => {
if (err) {
// error
} else {
// success
}
})
IMHO promise-based APIs are strictly better than callback/“errback” style APIs for many reasons, async/await support in the language being the nail in the coffin.>A middle ground is to have both Promise and callback, eg. if(!cb) return promise.
This works in many cases, but becomes ugly in some. The 2 ways of working have subtle differences (callbacks and promises both trigger at different times, and there is some interplay with "microtasks" and queues and schedules and other things that make some situations hairy). There's also the problem of Promises being hard to use when additional closures are involved (it's why `.forEach` sees much less usage in async/await codebases, because it's a lot more cumbersome to "wait" on iterating over an entire array with `.forEach` because the inner `await` can't escape the function in `.forEach`.) So most users will end up choosing one or the other for the whole library, and at that point why not just have them be 2 different libraries?
It also makes documentation harder, and makes the implementation of the library a lot more complex (now it needs to do EVERYTHING in both a promise-friendly way, and a callback-friendly way), and some things just can't be easily split (how do you support both async iterators and node.js event streams at the same time in the same interface?)
Better than having
if (err) {
callback(err);
return;
}
at the top of every function.> It also makes it harder to do more advanced designs like rate limiting, loading bars, etc.
No it doesn't. Rate limiting is no harder with Promises or async/await. Loading bars can be handled with async iterators. At the worst, one only needs to fall back to callbacks in such situations, and abstract that in a Promise wrapper for use with async/await.
> I really like callback convention with error first.
It has completely different semantics from the rest of the language. You must handle an error wherever it's thrown, which is ridiculous.
> The most important part of async code is error handling.
Sure, and that handling should be done at the highest level possible.
With this philosophy in mind I've never been overburdened by try/catch littering my code.