Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any reason to prefer the async versions of functions in node.js while calling them from inside an async function?

Mainly looking at the FS api, for most functions it seems there are three flavors to choose from:

  1. Synchronous
  2. Asynchronous using callback
  3. Asynchronous using promises

Async is the superior way to use system resources, however, if I'm already inside an async function and am awaiting every call anyway then there shouldn't be any difference between that and just using synchronous calls, right? To me it just seems like a built in await statement.

I don't know how async is implemented in js/node though. Is there any advantage to using async functions if I'm inside an async function to begin with? (excluding scenarios when I'm running async tasks in parallel)

like image 658
user81993 Avatar asked Oct 21 '25 15:10

user81993


1 Answers

One should decide to use an async function ONLY based on what is going on inside that function, not on who is calling it. The caller does not affect whether a function should be async or not.

Reasons to make a function async:

  1. You have promise-based asynchronous operations inside the function and you wish to use await.
  2. You have promise-based asynchronous operations inside the function and you wish to take advantage of the automatic catching of synchronous exceptions (and conversion to a rejected promise) that might occur before you invoke your asynchronous operations.

And, that's pretty much it for the reasons to use the async keyword in front of a function.

Things that an async function is NOT (or common misconceptions about async functions):

  1. It doesn't magically make blocking code become non-blocking.
  2. It doesn't make the caller run faster.
  3. It doesn't make synchronous code now run in the background asynchronously.

Async is the superior way to use system resources,

Not sure what you mean there. Asynchronous functions allow you to run operations in a non-blocking fashion so that the main thread in Javascript can do other things while the asynchronous operation is running, but that is not something that an async function enables. That's enabled by an asynchronous operation. The two are different.

if I'm already inside an async function and am awaiting every call anyway then there shouldn't be any difference between that and just using synchronous calls, right?

Incorrect. Using await with a function that returns a promise does suspend execution of the current function (returning a promise immediately), but that allows the main Javascript thread to do other things, serve other requests, etc... Using synchronous code would be blocking and would not allow the main thread to do other things, thus ruining the scalability of a server.

To me it just seems like a built in await statement.

Blocking, synchronous code affects everything else that could be running during your operation. It's not the same as using await.

like image 149
jfriend00 Avatar answered Oct 24 '25 04:10

jfriend00