2

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)

user81993
  • 6,167
  • 6
  • 32
  • 64
  • 1
    synchronous calls will always prevent **all other** processing while waiting for the result - so, it's not relevant if it's inside an async function or not - read up on the event loop in javascript - https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop – Jaromanda X Feb 20 '20 at 22:07
  • Just to correct the assumption you seem to be operating on: asynchronicity does not imply concurrency in the same execution context. The concurrency that occurs here is invisible at the language-level. When you're in an asynchronous callback, or a promise continuation, that execution context is still blocking the thread from running any other JavaScript. – Patrick Roberts Feb 20 '20 at 22:17

2 Answers2

5

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
1

async/await is not equivalent to synchronous execution; it's syntactic sugar to make working with promises easier (indeed, the goal of these keywords is to make promises closer to writing synchronous code from a programming standpoint and therefore more intuitive to use).

async places a task on the event loop which will execute after all synchronous execution pending on the stack finishes. The advantages of this are clear: the process can do work while waiting for resources to be available (opening a file involves a system call, for example, so it makes sense to make this a non-blocking operation).

If you're already inside an asynchronous function, the advantages and drawbacks of asynchronous operations are just the same as they would be anywhere else. In fact, await can only be used in an async function.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
  • 1
    "Is there an advantage": yes, in most cases. Synchronous will just block up the event loop again. I'll try to make this clearer. – ggorlen Feb 20 '20 at 22:11