1

(Forgive me if the title is inaccurate to the problem this one is boggling the mind) My application requires that I check a value from the request against the database. For this I created an asynchronous function to query the database:

async function checktoken(){
    return prisma.$exists.invalidtoken({token: "testtoken"}).then(result => {
        return(result)
    })
}

I know that the database call on its own works:

prisma.$exists.invalidtoken({token: "testtoken"}).then(result => {
    console.log(result) // returns true 
})

In the function that fires at every request I try and call checktoken():

async function getUser(token){
    var promise = await checktoken()
    var result = promise 
    console.log(result) //undefined
};

Amending the function to include an explicit call to the database works but only when var promise = await checktoken() is defined before it:

async function getUser(token){
    var promise = await checktoken() //When this is removed result1 is undefinded

    await prisma.$exists.invalidtoken({token: "testtoken"}).then(result1 => {
        console.log("inside db call: "+result1) // true
    })
};

I think I have a fundamental misunderstanding of async/await but I am not sure exactly what I am missing.

EDIT: I have updated my approach taking the advice I received and it still does not work. I am beginning to think my ORM is doing something weird:

async function test(token) {
    const status = await prisma.$exists.invalidtoken({ token: token });
    console.log(status);
    return status;
}

test("123") //logs false (as it should)

async function getUser(token){
    var status = await test(token) //logs undefined
    console.log(status) //logs undefined
};
2
  • Yeah, you see the then callback is a function in itself, so the return keyword would return the callback rather than the getUser function. Wrap it in a promise and resolve it. Or as mentioned by @JamiesonRhyne, async/await without the then callback will work equally well. Commented Apr 20, 2019 at 3:01
  • 1
    .then(result => { return result }) demonstrates lack of understanding Commented Apr 20, 2019 at 3:16

1 Answer 1

1

An async function requires an await. If you are using the promise.then() technique, then what you would want to do is return a new Promise(), and within the .then call back function resolve the promise

function checktoken() { 
    return new Promise((resolve,reject) => {
        prisma.$exists.invalidtoken({token: "testtoken"}).then(result => { 
            doSomeOtherStuff();
            resolve(result);
        });
    });
}

Is functionally the same as

async checktoken() {
    await prisma.$exists.invalidtoken({token: "testtoken"});
}
Sign up to request clarification or add additional context in comments.

4 Comments

That's the promise constructor antipattern. The first one should simply be: function checktoken() { return prisma.$exists.invalidtoken({token: "testtoken"}); }. The second one is missing a return statement (and function keyword): async function checktoken() { return await prisma.$exists.invalidtoken({token: "testtoken"});, but there is no benefit in using an async function, except for reader clarity maybe.
I tried your second technique (with some added debugging) and I can't say it's working as it should. I am beginning to think my ORM is doing something weird.
I agree with Felix, only value in example one is if you need to perform some additional work, and not just return the result.
Dov, make sure you understand that with both promises, the subsequent lines of code will continue to execute, only upon completion will the code within the .then callback be fired, and the subsequent lines will keep running until then. As such, make sure that whoever is calling checktoken is either awaiting the result or putting dependent code in the callback, like checktoken().then((token)=> dodependentwork());

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.