【问题标题】:Javascript: SyntaxError: await is only valid in async functionJavascript:SyntaxError:等待仅在异步函数中有效
【发布时间】: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


【解决方案1】:

只能在 async 函数下运行 await 语句。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

所以,你可以写你的

}).then((app) => {

作为

}).then(async (app) => {

【讨论】:

  • 感谢您的回答!你是第一个,但在@estus 答案中的清晰度和深度,我将其标记为正确!
  • Yaaaaay 你成就了我的一天 :D :D :D
【解决方案2】:

addEventasync..await 和原始承诺的混合体。 awaitthen 的语法糖。它是一个或另一个。混合导致不正确的控制流; db.App.findOne(...).then(...) promise 没有被链接或返回,因此在 addEvent 外部不可用。

应该是:

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;
}

一般来说,简单的回调不应与承诺混合。 callback 参数表示使用addEvent 的API 可能也需要被promisified。

【讨论】:

    【解决方案3】:

    async/await 仅在立即函数具有 async 关键字时才有效,您需要这个:

    ...
        }).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
                }
        })
    

    【讨论】:

      【解决方案4】:

      您只能在异步函数中使用 await。您也只能等待一段返回承诺的代码。

      在这里,您在不同的上下文中使用 await。最好在这里使用 then() 来解决问题。

      await 仅在包含它的立即函数是异步的情况下才有效。

      【讨论】:

      • 这如何回答这个问题?您基本上只是重申了错误消息的内容。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      • 2021-01-11
      • 2019-07-15
      • 1970-01-01
      • 2022-01-25
      • 1970-01-01
      • 2020-12-14
      相关资源
      最近更新 更多