2

I've got these 2 functions

async function movieExists(movie){
        return new Promise((resolve, reject) => {
            const sql = '...';

            con.query(sql, (err,result,fields) => {
                if(err)
                    throw err;
                else if(!result[0])
                    reject("Error");
                else
                    resolve(result[0].total);
            })
        })
}
async function addMovie(movie){
        return new Promise((resolve, reject) => {
            const sql = '...';
            const exists = await movieExists(movie); //Error on this line

            if(exists == 0){
                con.query(sql, (err,result,fields) => {
                    if(err)
                        reject("Error");
                    else   
                        resolve("Success");
                })
            }else
                resolve("Movie already exists");
        })
}

Error : SyntaxError: await is only valid in async function

Node version -> v10.16.0

**If you have any suggestions for the code itself I would be glad to hear them.

4
  • 3
    (resolve, reject) => { that's the function that isn't async ... the fact that it's a promise constructor also suggests you're guilty of the Promise constructor anti-pattern ... you already have something that returns a promise ... you don't need to construct a promise Commented Mar 25, 2020 at 2:22
  • 1
    move the new Promise line down two lines - problem solved Commented Mar 25, 2020 at 2:23
  • 2
    What is con? Most DB connection libraries on NPM now offer promise-based APIs. Would save you a lot of headache and no more new Promise(... Commented Mar 25, 2020 at 2:26
  • 1
    I'd actually suggest switching to the musql2 version of your database which has built-in promise support. Then, you can use promises for all your database operations. Commented Mar 25, 2020 at 2:43

2 Answers 2

3

The await keyword is directly inside the function that you are passing to new Promise(), which is not async.

Try moving it outside that function, like this:

async function addMovie(movie){
  const exists = await movieExists(movie);
  return new Promise((resolve, reject) => {
    const sql = '...';
    // ...
  })
}
Sign up to request clarification or add additional context in comments.

1 Comment

On the Promise antipattern others are talking about you might want to look at util.promisify() (added in node 8.0.0)
1

The second condition is wrong because in the Promise the anonymous function is not async function you can use the following instead, but it is not a good way to control the sequence of async.

async function addMovie(movie){
        return new Promise(async (resolve, reject) => {
            const sql = '...';
            const exists = await movieExists(movie); //Error on this line

            if(exists == 0){
                con.query(sql, (err,result,fields) => {
                    if(err)
                        reject("Error");
                    else   
                        resolve("Success");
                })
            }else
                resolve("Movie already exists");
        })
}

Comments

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.