Saturday 10 February 2018

Getting really confused about what should be async, Promise

So, I am writing a REST API with node.js. It's been fun so far but I am getting really really confused (and a little bit annoyed). I have this code: linkI created a route /user/register with express and its only task is to call an async function. The function queries and inserts into a database (mysql2/promise). I am using await and everything is working as expected.At the end of the function I am calling userToEvents function that is eventually going to have to return some kind of information. It has to be separate because I am going to have to use the same code elsewhere later.The last bit of code should iterate through an array of ids and checks if it is already in the table (it is a join table). And this is the point where I gave up.I tried annotating async the userToEvents method and use await like I do in registerUser function but it didn't work (I guess because the await was inside the Promise). I would like this function to be asynchronous like the rest of the code, but I don't know what to do. I guess it should return a Promise and I should await for it in registerUser. If I am right, what kind of code should be inside the Promise? Can it be synchronous code since it's in a promise that is already asynchronous?

Submitted February 10, 2018 at 07:29PM by ftibi93

No comments:

Post a Comment