People say that async/await
is just another way to write callbacks for promises. However, I feel as though they are not interchangeable; in fact, callbacks would be better in Express because it returns the result to the user right away. Is this correct?
In the following example, let's say we don't care about the result of doSomethingAsync()
, but want to redirect right away. Here, we use await
.
router.get('/register', async (req, res) => {
res.redirect('/')
await doSomethingAsync()
console.log(1)
}
router.get('/', (req, res) => {
console.log(2)
...
}
This would print out 1
, then 2
, meaning that we won't be able to exit the /register
handler and redirect unless the async function finishes.
In the below version, we use a promise with a callback in then
.
router.get('/register', (req, res) => {
res.redirect('/')
doSomethingAsync().then(() => {
console.log(1)
})
}
router.get('/', (req, res) => {
console.log(2)
...
}
This would print out 2
, then 1
, meaning that we can exit immediately and redirect the user.
Isn't the second way better for performance? Or are they actually the same?
Async functions not only allow the programmer to escape from callback hell and promise chaining in asynchronous code, but they also make the code seemingly synchronous. Ace your System Design Interview and take your career to the next level.
So from my findings i assure you ES6 promises are faster and recommended than old callbacks.
The disadvantage of callbacks is that it requires a different mode of thinking than some programmers are used to (to be fair, so does correct multi-threading). You do also lose access to a proper stack trace when using callbacks because they're executed async.
Conclusion. To implement asynchronous code in JavaScript we use callback functions and promises. A callback function is passed as an argument to another function whereas Promise is something that is achieved or completed in the future.
People say that
async/await
is just another way to write callbacks for promises. However, I feel as though they are not interchangeable;
That is incorrect, and they are indeed interchangeable. In fact, async/await
allows you to do things more easily than with plain promises, since an await
expression can be inserted into virtually any control flow logic seamlessly, while you cannot do so with plain promises, and are required to chain them using sequential .then()
calls.
In fact, callbacks would be better in Express because it returns the result to the user right away. Is this correct?
No. A callback is a different mechanism for delivering data asynchronously; it is not "right away", nor is it significantly faster, and the benefits of programming with promises far outweigh any negligible performance implications of resorting to callback hell. In addition, your second example is not a true callback, it is a promise chain.
This would print out
1
, then2
, meaning that we won't be able to exit the/register
handler and redirect unless the async function finishes.
That is incorrect. await
is non-blocking. The async
function returns to the caller as soon as doSomethingAsync()
is invoked, and the async
function's return value is a promise that is resolved after console.log(1)
has been invoked.
This would print out
2
, then1
, meaning that we can exit immediately and redirect the user. Isn't the second way better for performance? Or are they actually the same?
The two snippets are algorithmically the same, and since the console.log(2)
is dependent on the client's browser performing the HTTP request indicated by res.redirect('/')
which is independent of doSomethingAsync()
, you actually have a race condition, and neither order of logs is guaranteed. Its performance is also no different than the example using await
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With