D
D
Denis Yakovenko2015-10-31 16:13:41
JavaScript
Denis Yakovenko, 2015-10-31 16:13:41

How to pull the value of a variable from a nested await call to an async function in ES7?

I'm trying to get a boolean value from asynca function that has a nested anonymous asyncfunction in it. Perhaps it will be clearer in the code:

async userExistsInDB(email) {
    let userExists;
    await MongoClient.connect('mongodb://127.0.0.1:27017/notificator', async(err, db) => {
        if (err) throw err;

        let collection = db.collection('users');
        userExists = await collection.find({email: email}).limit(1).count() > 0;
        console.log("INSIDE:\n", userExists);
        db.close();
    });
    console.log("OUTSIDE:\n", userExists);
    return userExists;
}

The function itself is inside a class that has another function that calls userExistsInDB :
async getValidationErrors(formData) {
   let userExists = await this.userExistsInDB(formData.email);
   console.log("ANOTHER FUNC:\n", userExists);
}

If I run the getValidationErrors function , I get the following output on the console:
OUTSIDE:
undefined
ANOTHER FUNC:
undefined
INSIDE:
true

although when using await , INSIDE: true
is expected to be printed first. Basically, I just need to get the value of the userExists variable from the nested asynchronous call in the userExistsInDB() function and return it. But for some reason I can't figure out what I'm doing wrong.
I hope someone can help me

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2015-10-31
@yakovenkodenis

I'm not 100% sure but... from what you wrote:
you need to remove the callback. Regarding error handling from the async-functions specification, we should rewrite the function like this:

async userExistsInDB(email) {
    let userExists; 
    try {
         const db = await MongoClient.connect('mongodb://127.0.0.1:27017/notificator');
    } catch (err) {
         throw err; // тут у вас никакой обработки ошибок небыло... можно и без try/catch тогда
    }

    const collection = db.collection('users');
    userExists = await collection.find({email: email}).limit(1).count() > 0;
    console.log("INSIDE:\n", userExists);
    db.close();
    
    console.log("OUTSIDE:\n", userExists);
    return userExists;
}

The code can be further simplified further. The bottom line is that when using await there is no point in callbacks. Everything happens synchronously (from the point of view of the flow of control, of course there will be no blocking).

D
Denis Yakovenko, 2015-10-31
@yakovenkodenis

It turned out like this in the end:

async userExistsInDB(email) {
        let db = await MongoClient.connect('mongodb://127.0.0.1:27017/notificator');
        try {
            let collection = db.collection('users');
            let userCount = (await collection.find({email: email}).limit(1).count());
            return userCount > 0;
        } finally {
            db.close();
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question