<i id='gcvK0'><tr id='gcvK0'><dt id='gcvK0'><q id='gcvK0'><span id='gcvK0'><b id='gcvK0'><form id='gcvK0'><ins id='gcvK0'></ins><ul id='gcvK0'></ul><sub id='gcvK0'></sub></form><legend id='gcvK0'></legend><bdo id='gcvK0'><pre id='gcvK0'><center id='gcvK0'></center></pre></bdo></b><th id='gcvK0'></th></span></q></dt></tr></i><div id='gcvK0'><tfoot id='gcvK0'></tfoot><dl id='gcvK0'><fieldset id='gcvK0'></fieldset></dl></div>

    <legend id='gcvK0'><style id='gcvK0'><dir id='gcvK0'><q id='gcvK0'></q></dir></style></legend>
  1. <small id='gcvK0'></small><noframes id='gcvK0'>

      <tfoot id='gcvK0'></tfoot>
      • <bdo id='gcvK0'></bdo><ul id='gcvK0'></ul>
    1. Javascript: SyntaxError: await 仅在异步函数中有效

      时间:2023-09-03
      <tfoot id='ja1aP'></tfoot>
        <tbody id='ja1aP'></tbody>

        • <legend id='ja1aP'><style id='ja1aP'><dir id='ja1aP'><q id='ja1aP'></q></dir></style></legend>

          <small id='ja1aP'></small><noframes id='ja1aP'>

          <i id='ja1aP'><tr id='ja1aP'><dt id='ja1aP'><q id='ja1aP'><span id='ja1aP'><b id='ja1aP'><form id='ja1aP'><ins id='ja1aP'></ins><ul id='ja1aP'></ul><sub id='ja1aP'></sub></form><legend id='ja1aP'></legend><bdo id='ja1aP'><pre id='ja1aP'><center id='ja1aP'></center></pre></bdo></b><th id='ja1aP'></th></span></q></dt></tr></i><div id='ja1aP'><tfoot id='ja1aP'></tfoot><dl id='ja1aP'><fieldset id='ja1aP'></fieldset></dl></div>

            <bdo id='ja1aP'></bdo><ul id='ja1aP'></ul>

                本文介绍了Javascript: SyntaxError: await 仅在异步函数中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我在 Node 8 上使用 Sequelize.js

                I am on Node 8 with Sequelize.js

                尝试使用 await 时出现以下错误.

                Gtting the following error when trying to use await.

                SyntaxError: await 仅在异步函数中有效

                代码:

                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
                

                我做错了什么?

                推荐答案

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

                addEvent is a mixture of async..await and raw promises. await is syntactic sugar for then. It's either one or another. A mixture results in incorrect control flow; db.App.findOne(...).then(...) promise is not chained or returned and thus is not available from outside 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.

                Generally plain callbacks shouldn't be mixed with promises. callback parameter indicates that API that uses addEvent may need to be promisified as well.

                这篇关于Javascript: SyntaxError: await 仅在异步函数中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:续集关联 - 请改用 promise 样式 下一篇:sequelize, Statement(where) in statement(where)

                相关文章

                  <bdo id='qnvmP'></bdo><ul id='qnvmP'></ul>
                    <tfoot id='qnvmP'></tfoot>

                    <small id='qnvmP'></small><noframes id='qnvmP'>

                  1. <i id='qnvmP'><tr id='qnvmP'><dt id='qnvmP'><q id='qnvmP'><span id='qnvmP'><b id='qnvmP'><form id='qnvmP'><ins id='qnvmP'></ins><ul id='qnvmP'></ul><sub id='qnvmP'></sub></form><legend id='qnvmP'></legend><bdo id='qnvmP'><pre id='qnvmP'><center id='qnvmP'></center></pre></bdo></b><th id='qnvmP'></th></span></q></dt></tr></i><div id='qnvmP'><tfoot id='qnvmP'></tfoot><dl id='qnvmP'><fieldset id='qnvmP'></fieldset></dl></div>

                    <legend id='qnvmP'><style id='qnvmP'><dir id='qnvmP'><q id='qnvmP'></q></dir></style></legend>