2

I am fairly new to node and using express with it.


I have a simple API which gets data from mongo in this way:

  1. From a Router I call a async method in AccountServices(which is in a different file) called getAccountDetails
  2. getAccountDetails in turn gets data from mongo making an async call.
  3. I don't want to pass req,res objects to the getAccountDetails function, So I've use async await and it works perfectly.

What I want to understand is does my whole thread/api goes into wait state for that async await function to resolve?

If So, how can I overcome this.

Mohit Agarwal
  • 201
  • 2
  • 10
  • 4
    No, it does not. Consider async-await as syntactic sugar for promises. It works like a promise but looks like a synchronous code – Ishwar Patil Jun 19 '20 at 14:38
  • Does this answer your question? [Will async/await block a thread node.js](https://stackoverflow.com/questions/46004290/will-async-await-block-a-thread-node-js) – ponury-kostek Jun 19 '20 at 19:25

1 Answers1

1

async/await functions are asynchronous which means that the rest of your code doesn't wait for them to finish. They are very useful for operations like fetching data since you can't predict the time it'll take which would potentially end up freezing your app for seconds or even minutes.

maxeth
  • 1,315
  • 1
  • 8
  • 17