<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 仅在异步函数中有效

      Javascript: SyntaxError: await is only valid in async function(Javascript: SyntaxError: await 仅在异步函数中有效)
      <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 仅在异步函数中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Pause youtube video, youtube api(暂停 youtube 视频,youtube api)
                Youtube iframe api not triggering onYouTubeIframeAPIReady(Youtube iframe api 未触发 onYouTubeIframeAPIReady)
                How can I stop a video with Javascript in Youtube?(如何在 Youtube 中停止使用 Javascript 的视频?)
                How to call Greasemonkey#39;s GM_ functions from code that must run in the target page scope?(如何从必须在目标页面范围内运行的代码中调用 Greasemonkey 的 GM_ 函数?)
                How do you mute an embedded Youtube player?(如何使嵌入式 Youtube 播放器静音?)
                How to get number of video views with YouTube API?(如何使用 YouTube API 获取视频观看次数?)
                  <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>

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