'Is it ever better to use Node's filesystem sync methods over the same async methods?

This is a question about performance more than anything else.

Node exposes three different types of methods to accomplish various filesystem tasks:

  • Promises API (async)
  • Callback API (async)
  • Synchronous API (sync)

I've read more articles and stackoverflow answers than I can count, all of which claiming to never need the sync methods.

I recently wrote a script which required a couple directories to be made if they didn't already exist. During this, I noticed that if I used the async/await methods (primarily fs.promises.mkdir and fs.promises.access), the event loop would simply continue to the next async bit of code, regardless of the fact that the next bits require those directories. This is expected behavior, after all, it's async.

I understand this could be solved with a nice little callback hell sesh, but that isn't the question, whereas the idea that the promises api can be used over all other methods is.

The question then becomes:

Is it ever better to use Node's filesystem sync methods over the same async methods?

Is it ever truly required in situations like this to block the process?

Or said differently:

Is it possible to completely avoid sync methods and ONLY use the promises api (NOT promises + callbacks)?

It seems like using the sync methods (given my situation above, where the directories are required to be there before any other call is made) can be EXTREMELY useful to write readable, clear code, even though it may negatively impact performance.

With that being said, there's an overwhelming level of information to say that the sync api is completely useless and never required.

Again, this purely caters to the promises api. Yes, callbacks and promises are both async, but the difference between the job and message queues makes the both api's completely different in this context.

PS: For additonal context on examples, I've provided a code sample so you don't have to imagine my example ;)

Thanks! :)

// Checks if dir exists, if not, creates it. (not the actual code, just an example)

// Sync version
if (!fs.existsSync(dirPath)) {
     fs.mkdirSync(dirPath);
}

// Async version
  try {
    await fs.promises.access(dirPath);
  } catch {
    await fs.promises.mkdir(dirPath);
  }


Solution 1:[1]

Adding flex-direction: column to the css for your itemsContainer class should put them in a vertical list

.itemsContainer {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ryan Walls