我正在使用Sequelize.js和Node 8。
在尝试使用await
时,出现以下错误:
SyntaxError: await is only valid in async function
代码:
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
}
})
}
得到以下错误:
let result = await promise;
^^^^^
SyntaxError: await is only valid in async function
我做错了什么?
then((app) => {
这个匿名函数没有标记为async
。 - skyboyerthen
回调函数中直接返回Promise并将其移至链的下方,而不是等待它... - Clint