Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4076372
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T17:25:00+00:00 2026-05-20T17:25:00+00:00

I’ve been starting to play around with Node.js recently and I’ve come across a

  • 0

I’ve been starting to play around with Node.js recently and I’ve come across a situation where I need a little guidance around the prescriptive node.js way of accomplishing a task. In this particular case I need to create a bunch of directories and, when all of the directories have been created I need to perform some final operation. The order in which the directories are created does not matter, I just need to perform a final operation after the last one is.

The easiest way the accomplish this would be to fall back to the old synchronous habits. That is, just call fs.mkdirSync for each of the directories and perform the operation at the end. For example:

fs.mkdirSync('a', 0755);
fs.mkdirSync('a/b', 0755);
fs.mkdirSync('a/b/c', 0755);
performFinalOperation();

While this would work, it doesn’t feel like it’s the node.js way of doing it. Obviously, the program would block while it waits for the OS to create the directory and return. On a heavily loaded system with a file system that’s mounted remotely each of the mkdirSync calls could take a very long time. So clearly, this isn’t the best approach.

One of Node.js main selling points is the fact that it’s asynchronous. So the calls to fs.mkdir could be chained through the callbacks:

fs.mkdir('a', 0755, function(e) {
    if (!e) {
        fs.mkdir('a/b', 0755, function(e) {
            if (!e) {
                fs.mkdir('a/b/c', 0755, function(e) {
                    if (!e) {
                        performFinalOperation();
                    }
                });
            }
        });
    }
});

Again, this approach I’m sure works, but it leads to really deep nesting and code duplication. It does have the benefit of not blocking while the directories are created, but at what cost?

Another approach would be to get really fancy in an effort to avoid the code duplication and nesting:

(function (directories) {
    if (directories.length === 0) {
        performFinalOperation();
    } else {
        var tail = arguments.callee;
        fs.mkdir(directories.shift(), 0755, function(e) {
            tail(directories);
        });
    }
})(['a', 'a/b', 'a/b/c']);

This approach makes use of all sorts of crazy stuff: anonymous self calling functions and the magical arguments.callee. But worst of all, it isn’t obvious what the code is doing at first glance.

So, while the concrete question is around creating directories I’m more interested in the approach that a seasoned node.js veteran would take when this sort of situation arises. I’m specifically not interested in what libraries are around to make this easier.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T17:25:00+00:00Added an answer on May 20, 2026 at 5:25 pm

    Oh, hey Bryan 🙂

    I’m specifically not interested in
    what libraries are around to make this
    easier.

    The seasoned Node veteran has written at least one of their own control flow libs. We just copied Twisted’s Deferred classes since they already did the hard work and research into async programming. This inverts the standard callback-as-argument pattern and I like the resulting code, but if you want to nest a bunch of callbacks you can still do that with Deferred and end up with just as much of a mess.

    With the restriction of not using libraries people generally do exactly what you wrote. There really isn’t any other choice. Without language changes such as generators the best we can do is use libraries. If you don’t want to use an existing one you will end up rolling your own or just writing a lot of boilerplate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm looking for suggestions for debugging... If you view this site in Firefox or
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.