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: 
As @jfriend00 mentioned, you need to research about asynchronous operations.

I refactored your code using promises for the asynchronous calls. You could make it a little bit easier to read by using await.

```
const queryUsers = (db) => new Promise ((resolve, reject) => {
    const query = "SELECT * FROM users";

    db.query(query, (error, results, fields) => {
        if (error) {
            return reject(error);
        }

        resolve(results);
    });
});

const getUserFiles = username => new Promise((resolve, reject) => {
    fs.readdir(path.join(__dirname,'..', 'userdata', username), (err, entries) => {
        if (err) {
            return reject(err);
        }

        resolve(entries);
    });
});

const getUsersFiles = users => Promise.all(
    users.map(user => getUserFiles(user.username)
        .then(files => ({ ...user, dir: files })))
);

router.get('/', function(req, res, next) {
    queryUsers(req.db)
        .then(getUsersFiles)
        .then(users => {
            console.log(users);
            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);
 }

```