Issue
I wrote the code below for a project where I do everything user-related. Here I have a small part of the code inside my service file.
let result: User | null = await userModel.registerUser();
return result;
As feedback from my group, I received: "Use callback rather than await for better practice.". This really confused me because why would a callback function be better than simply using await. Once the response is given by userModel.registerUser() the result will be returned to the controller and everything works perfectly. Await takes up less space and the readability is a lot better to me.
I have changed my code to this (error handling is also done within userMode.registerUser() according to the response from the datababase hence the _):
userModel.registerUser(function(_, result) {
return result;
});
This works but I don't see how this is better.
Solution
Disclosure: I would need more context to give you a strait answer to which is better and why.
The initial code you provided uses async/await, which IS a more modern and readable approach to dealing with asynchronous operations.
However, there are cases where using callbacks might be preferred, better or even required depending on the context and specific requirements of your project.
For example if you are changing state or needing to cache a function to avoid unnecessary re-renders, callbacks are made specifically to solve those issues, see documentation here
But if you are just waiting for a response such as an api call, then async/await is definitely better.
Again I would need to see the code implementation in order to give u a definitive answer on which one if better for your specific use case but here are some other considerations for both approaches:
Async/Await:
- Readability: Async/await code tends to be more readable and resembles synchronous code, making it easier to understand.
- Error Handling: With async/await, you can use try/catch blocks for better error handling, making it more straightforward to handle exceptions.
Callbacks:
- Compatibility: In some scenarios or libraries, callbacks might be the preferred way to handle asynchronous operations.
- Control Flow: Callbacks can provide more control over the flow of your program in certain situations.
In your case, if coworkers feedback suggests using callbacks, it might be beneficial to understand the specific reasons behind it. Callbacks can be appropriate or better in specific scenarios, but generally and typically using async/await is considered a more modern and readable approach.
If your code with async/await is working well and is easy to understand, there is probably not a strong need to switch to callbacks.
Also take note if your coworkers are legacy developers(like the old dudes that have always worked there) it could be a case where they may assume a callback should be used because they do not know better or have always done it that way because callbacks have been around for a while. Which is why I also suggest bringing this question up again to have a healthy debate on which is better, just make sure to do some research so you can explain why async / await is better - that way you can help them become modern developers!
Answered By - Kyle Xyian Dilbeck
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.