【发布时间】:2018-10-25 02:01:31
【问题描述】:
我在节点 8 上,Sequelize.js
尝试使用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 -
在
then回调中返回承诺并将其移到链下而不是等待它可能会更好...... -
我觉得你试图解决承诺的方式有点奇怪,为什么你不使用 .then() ?
标签: javascript node.js async-await sequelize.js es6-promise