Doing JS for ages and was just working on some koa middleware that writes into a database and waits for a confirmation before sending a 201 to the client.Of course, I had missed to put some awaits in front of some async functions, so koa fireds 404 before the database could react.It took me quite some while to get some working code because:Using promises (.then) is good to let the function in the.thenexecute once the function before returned something BUT this seems to be not blocking if done inline in the middleware; if the function in the.thenisn't async as well paired with some more awaits, koa fires quickly its 404 before anything done =>.then gives you order control within but the Promise itself is, of course, asyncSo async/await is better there because you can actually block but code gets quickly clumsy and full of awaits + extra `if` statementsSome stuff like if statements, declarations, a JSON.parse are synchronous, actually entire JS is sync except timers and events; even if it's easy to remember, still I am adding random awaits anywhere because I am just to lazy too thinkOnce you read again about all this stuff and you bring some time you get there. But it's not intuitive and every time, I need to read about all this promise/async/await stuff again and swear that I'll write the next backend in Go.What do you think? What do I wrong? How can I get this whole thing more intuitive without spending too much time on it? Or is Go the answer?
Submitted August 04, 2019 at 01:49PM by po35
No comments:
Post a Comment