【问题标题】:await is not a reserved wordawait 不是保留字
【发布时间】:2019-08-03 22:40:23
【问题描述】:

这是我的代码,

exports.prepareMeshTerms = function(req,res) {
var input = req.body,
start = input.start,
limit = input.limit,
count = 0,
pipeData = '';
MeshTerms.find().skip(start).limit(limit).exec(function (err, data) {
    if(err) {
        throw err;
    }
    if (data) {
        (async function(){
            data.map(element => {
                var string = element.Intervention.replace("|"," "),
                payload = { json: { input: element.Intervention } };
                await execute(payload,count,pipeData,element);
            });

        })();
    }
});};

它说 await 不是保留关键字。这里我在自调用函数中保持异步,但仍然抛出错误。谁能建议我帮助。谢谢。

【问题讨论】:

  • 您在 Node.js 上运行哪个版本?

标签: javascript node.js ecmascript-6 async-await


【解决方案1】:

您无法将map 与内部的await 同步。而是使用 Promise.all 创建一个 map 的异步函数:

        await Promise.all(data.map(async (element) => {
            var string = element.Intervention.replace("|"," "),
            payload = { json: { input: element.Intervention } };
            await execute(payload,count,pipeData,element);
        }));

【讨论】:

    猜你喜欢
    • 2017-05-24
    • 2019-03-05
    • 2019-03-28
    • 1970-01-01
    • 2017-02-08
    • 2017-12-08
    • 2018-11-08
    • 2017-07-07
    相关资源
    最近更新 更多