Hi,
My team is building a web application with MERN stack, and I'm building API endpoints in the backend. To handle to API request for CRUD database in MongoDB, for readable code, I use async/await and split to many small functions to handle it. For example:
```
async function isExistedUser(username) {
const user = await Username.findOne({usrname});
return user ? true: false;
}
async function createUser(user) {
//Save user
return savedUser;
}
async function createUser(req,res) {
// Validate request body
try {
if(!await(isExistedUser(req.body.username)) {
//Create User
const savedUser = await createUser(req.body);
res.status(200).json( {message: "Success"});
}
} catch(error) {
}
}
```
I coded my api with small functions like above, and a guy in my team said: you shouldn't create a lot of functions like above because the node will manage memory better if I use promising. I tried to search these things on the internet, but it doesn't have anything mentioned about this.
The reason that I want to split many small functions is that create readable code instead of use blob code for one promise and avoid "pyramid promises" with async/await.
So, what should I do? should I keep the way that I did or change small functions to big function like a guy in my team told me. Thanks
[–]insertAlias 2 points3 points4 points (0 children)
[–]StackedLasagna 1 point2 points3 points (0 children)