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.
(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 promisenew Promiseline down two lines - problem solvedcon? Most DB connection libraries on NPM now offer promise-based APIs. Would save you a lot of headache and no morenew Promise(...