Question: I try to push the users array with the dirs. But the `dir` property is still blank in the console.log statement after this.
Have an Idea? I know i could just use a synchronus function but friend told me, synchronus functions are blocking all other stuff going on.

```
    router.get('/', function(req, res, next) {
    const db = req.db;
    const query = "SELECT * FROM users";
    const users = [];

    db.query(query, (error, results, fields) => {
        for(let i = 0; i < results.length ; i++){
            users.push(results[i]);
            users[i].dir = [];
            fs.readdir(path.join(__dirname,'..', 'userdata', results[i].username), (err, entries) => {
                users[i].dir.push(entries[i]);
            });

        }
        console.log(users);
        res.render("admin", {
            user: req.session.user,
            dbUsers: users,
        });
    });
});

```

So i just updated my code. But it dont works either.

Answer: 
`fs.readdir` is async function, which means that the loop will not wait until the `readdir` function finishes and then continue to the next iteration, try to use `fs.readdirSync` to make it synchronous, or you can use [async](https://github.com/caolan/async) module:

```
for(let i = 0; i < results.length ; i++){
    users.push(results[i]);

    let entries = fs.readdirSync(path.join(__dirname,'..', 'userdata', results[i].username));

    users[i].dir = entries;
}

```

[Async module](https://caolan.github.io/async/docs.htm) solution:

```
async.eachOf( results, function(user, i, next) {

    users.push(user);

    fs.readdir(path.join(__dirname,'..', 'userdata', user.username), (err, entries) => {

        // if error pass it to the last callback
        if (err) return next(err);

        user.dir = entries;

        // iteration done
        next();
    });

}, function (err) {

    // this function is executed only when all iteration done

    if (err) {
        throw err;
        return res.send('error occured');
    }

    res.render("admin", {
        user: req.session.user,
        dbUsers: users,
    });

});

```
Can you console out the value of `i` in this line `users[i].dir.push(entries[i]);` I'm going to guess that it is the max numbered item +1 every time, because the `fs.readdir` call is asynchronous, the value of `i` will not match up with what is expected. try using a value in a closure like this. when the closure is created the value of `i` is saved to the closure as `idx`.

```
 for(let i = 0; i < results.length ; i++){
        (idx => {
            users.push(results[idx]);
            users[idx].dir = [];
            fs.readdir(path.join(__dirname,'..', 'userdata', results[i].username), (err, entries) => {
                users[idx].dir.push(entries[idx]);
            });
        })(i);
 }

```