8

I am on Node 8 with Sequelize.js

Gtting the following error when trying to use await.

SyntaxError: await is only valid in async function

Code:

async function addEvent(req, callback) {
    var db = req.app.get('db');
    var event = req.body.event

    db.App.findOne({
        where: {
            owner_id: req.user_id,
        }
    }).then((app) => {

                let promise = new Promise((resolve, reject) => {
                    setTimeout(() => resolve("done!"), 6000)

                })

               // I get an error at this point 
               let result = await promise;

               // let result = await promise;
               //              ^^^^^
               // SyntaxError: await is only valid in async function
            }
    })
}

Getting the following error:

               let result = await promise;
                            ^^^^^
               SyntaxError: await is only valid in async function

What am I doing wrong?

3
  • 4
    then((app) => { this anonymous function is not marked as async Commented Oct 24, 2018 at 17:49
  • 2
    Probably better to just return the promise in the then callback and move it down the chain rather than awaiting it... Commented Oct 24, 2018 at 17:50
  • I find it kinda wierd the way your trying to get the promise resolve, why your not using .then() ? Commented Oct 24, 2018 at 17:50

4 Answers 4

18

You can run await statement only under async function. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

So, you can write your

}).then((app) => {

as

}).then(async (app) => {
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer! You were the first, but b/c of clarity and depth in @estus answer, I marked it as correct!
Yaaaaay you made my day :D :D :D
5

addEvent is a mixture of async..await and raw promises. await is syntactic sugar for then. It's either one or another. A mixture results in incorrect control flow; db.App.findOne(...).then(...) promise is not chained or returned and thus is not available from outside addEvent.

It should be:

async function addEvent(req, callback) {
    var db = req.app.get('db');
    var event = req.body.event

    const app = await db.App.findOne({
        where: {
            owner_id: req.user_id,
        }
    });

    let promise = new Promise((resolve, reject) => {
        setTimeout(() => resolve("done!"), 6000)
    })

    let result = await promise;
}

Generally plain callbacks shouldn't be mixed with promises. callback parameter indicates that API that uses addEvent may need to be promisified as well.

Comments

3

async/await only works if the immediate function has the async keyword, you need this:

...
    }).then(async app => {   // <<<< here

                let promise = new Promise((resolve, reject) => {
                    setTimeout(() => resolve("done!"), 6000)

                })

               // I get an error at this point 
               let result = await promise;

               // let result = await promise;
               //              ^^^^^
               // SyntaxError: await is only valid in async function
            }
    })

Comments

1

You can use await only inside a function which is async. Also you can await only a piece of code that returns a promise.

Here you are using await inside a different context. Better you use then() here to solve the problem.

await only works if the immediate function that encloses it is async.

1 Comment

How does this answer the question? You basically just restated what the error message says.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.