# Explanation needed on Node.js and the stack

**URL:** <https://forum.freecodecamp.org/t/explanation-needed-on-node-js-and-the-stack/374679>\
**Category:** General\
**Created:** [April 17, 2020, 12:15pm UTC](https://forum.freecodecamp.org/t/explanation-needed-on-node-js-and-the-stack/374679 "2020-04-17T12:15:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![FinnPegler](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/finnpegler/32/143339_2.png) [@FinnPegler](https://forum.freecodecamp.org/u/FinnPegler)\
**Post date:** [April 17, 2020, 12:15pm UTC](https://forum.freecodecamp.org/t/explanation-needed-on-node-js-and-the-stack/374679/1 "2020-04-17T12:15:33Z")

</div>

I have some code taken from a tutorial, and it works, but from my understanding of the stack and callback functions it shouldn’t. The code creates a directory called stuff, then creates a new file and writes a short array to that file.

Doesn’t the mkdir function get added to the stack first, and then the writeFile function? Meaning that the writeFile function is the first to fire, and it should fail as ./stuff doesn’t exist yet?

Can some please explain?

```auto
let arr2 = [1,2,3,4];
fs.mkdir("stuff", function (err){
    if (err) throw err;
    fs.writeFile("./stuff/newfile.txt", arr2, function (err){
        if (err) throw err;
    })
})

```

---

<div class="post-metadata">

**Author:** ![snigo](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/snigo/32/128765_2.png) [@snigo](https://forum.freecodecamp.org/u/snigo)\
**Post date:** [April 17, 2020, 12:47pm UTC](https://forum.freecodecamp.org/t/explanation-needed-on-node-js-and-the-stack/374679/2 "2020-04-17T12:47:51Z")

</div>

Callback in `.mkdir` will be invoked only when it’s finished with creating the directory. In other words `.writeFile` will be added on top of `.mkdir` when the directory will exist, not immediately.

---

<div class="post-metadata">

**Author:** ![FinnPegler](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/finnpegler/32/143339_2.png) [@FinnPegler](https://forum.freecodecamp.org/u/FinnPegler)\
**Post date:** [April 17, 2020, 1:11pm UTC](https://forum.freecodecamp.org/t/explanation-needed-on-node-js-and-the-stack/374679/3 "2020-04-17T13:11:08Z")

</div>

Thanks! Makes sense, appreciate it 🙂

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:58pm UTC](https://forum.freecodecamp.org/t/explanation-needed-on-node-js-and-the-stack/374679/4 "2021-06-01T18:58:25Z")

</div>

Closed on June 1, 2021, 6:58pm UTC


