3 ms·
Extracting callbacks into separate functions is just more indirection. And they don't solve the core issue of async control flow that async/await solves, so it
by hombre_fatal 2y ago
Extracting callbacks into separate functions is just more indirection.
And they don't solve the core issue of async control flow that async/await solves, so it's not an alternative to async/await much less a superior one.
A classic example is when you want to conditionally do something asynchronously like B() in this case.
function process(id, callback) {
A(id, (result) => {
if (result === 3) {
B(() => {
C(result, callback);
});
} else {
C(result, callback);
}
});
}
Versus:
async function process(id) {
const result = await A(id);
if (result === 3) {
await B();
}
return C(result);
}
Add a couple more layers of this and the async/await function stays simple and flat, and the callback version grows significantly more complex and nested.
- mu53 2y agoWithout the async keyword, it was far superior to anons. Also, named functions showed up on stack traces making it easy to know what is getting called No serious person is going to claim that any form of callbacks is superior to async/wait
- gibbitz 2y agoDon't forget having to use try... catch for exception handling. This is why promises are the one true way!