Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will await keyword in javascript slow down the application?

I am relatively new to NodeJS. I was wondering if the await keyword will slow down the entire javascript/nodeJS program?

For example,

If I have many express routers written on a single server file, and one router function calls the 'await' for a promise to resolve, will all others routers and asynchronous functions stay on halt/paused until the promise is resolved? Or just that thread will be paused?

In such a case the await call will cause performance issues to the Javascript program?

like image 614
Jab2Tech Avatar asked Sep 08 '25 13:09

Jab2Tech


2 Answers

No. While await sounds like it is blocking, it is fully asynchronous(non blocking) as it is also implied in the required function signature by async keyword. It is a (lot) nicer way to use promises. So go ahead and use it in your code.

You also mentioned threads, I suggest you ignore thread concept while developing node.js apps and trust the node.js event loop. Just never use blocking IO calls(which are explicitly named so by having 'Sync' in the name).

like image 148
Jaan Oras Avatar answered Sep 10 '25 07:09

Jaan Oras


await waits for a promise to get resolved, but as we know that node is asynchronous in nature, therefore, other requests will be made to the application will have no effect on them they will not wait for the previous request promise to be resolved.

example

route 1 -> it will await and iterate through million rows and return sum in response
route 2 -> it will only return '1' in response

now when we'll call route 1 first then route 2 then you'll see that still you'll get the response from route 2 immediately and when route 1 will get completed you'll get that response.

like image 28
arpitansu Avatar answered Sep 10 '25 06:09

arpitansu