Javascript Node js,如何在循环后运行代码同步

Javascript Node js,如何在循环后运行代码同步,javascript,node.js,mongodb,Javascript,Node.js,Mongodb,这是我试图更新数据库(MongoDB)的一些文档的代码 使用for循环。但是我想在循环完成后运行下一个代码, 例如,我想在循环完成后使用一些在循环内部计算的变量。 我如何使用回调、承诺等来实现这一点 numPktUpdated = 0; for (key in InvoicesPUT) { Invoice.findOneAndUpdate({ InvoiceNumber: InvoicesPUT[key].InvoiceNumber }, InvoicesPUT[key]).exe

这是我试图更新数据库(MongoDB)的一些文档的代码 使用for循环。但是我想在循环完成后运行下一个代码, 例如,我想在循环完成后使用一些在循环内部计算的变量。 我如何使用回调、承诺等来实现这一点

numPktUpdated = 0;
for (key in InvoicesPUT) { 
      Invoice.findOneAndUpdate({ InvoiceNumber: InvoicesPUT[key].InvoiceNumber }, InvoicesPUT[key]).exec()
      .then((doc) => {
           console.log("Update Succeeded")
           numPktUpdated = numPktUpdated + 1;
      })
      .catch((err) => {
            return resp.send(JSON.stringify({
            "status": "error",
            "message": "DB Error while Updating: Wrong Packet"
            }));
            console.log(err);
     })
}
resp.send(numPktUpdated);
这里,numPktUpdated=0被发送到客户机,尽管它在循环后的实际值是其他值


谢谢。

尝试将代码放入函数,然后使用异步运行它

    async function  forLoopFunction(args){//here is a for loop
    }

    let ret = await forLoopFunction(args);

尝试将代码放入函数中,然后使用async运行它

    async function  forLoopFunction(args){//here is a for loop
    }

    let ret = await forLoopFunction(args);

您应该能够使用
Promise.all()


您应该能够使用
Promise.all()


如果您使用的是较旧版本的Node v7.6,它提供了对该模式的支持,那么您可以使用一个简单的:


如果您使用的是较旧版本的Node v7.6,它提供了对该模式的支持,那么您可以使用一个简单的:


谢谢,只需稍作修改即可工作:

Promise.all(InvoicesPUT.map(invoice => {
                return Invoice.findOneAndUpdate({InvoiceNumber: invoice.InvoiceNumber}, invoice).exec()
                .then((doc) => {
                    console.log("Update Succeeded")
                    numPktUpdated = numPktUpdated + 1;
                })

            }))
            .catch((err) => {
                return resp.send(JSON.stringify({
                    "status": "error",
                    "message": "DB Error while Updating: Wrong Packet"
                }));
                console.log(err);
            })
            .then((resolve) => {
                return resp.send(JSON.stringify({
                    "status": "succed",
                    "LastSavedGUID": 654323,
                    "SyncDate": 1,
                    "pagenumber": numPktUpdated
                }));
            })

谢谢,只需稍作修改即可工作:

Promise.all(InvoicesPUT.map(invoice => {
                return Invoice.findOneAndUpdate({InvoiceNumber: invoice.InvoiceNumber}, invoice).exec()
                .then((doc) => {
                    console.log("Update Succeeded")
                    numPktUpdated = numPktUpdated + 1;
                })

            }))
            .catch((err) => {
                return resp.send(JSON.stringify({
                    "status": "error",
                    "message": "DB Error while Updating: Wrong Packet"
                }));
                console.log(err);
            })
            .then((resolve) => {
                return resp.send(JSON.stringify({
                    "status": "succed",
                    "LastSavedGUID": 654323,
                    "SyncDate": 1,
                    "pagenumber": numPktUpdated
                }));
            })

查看
async.series
在某些承诺返回后执行操作查看
async.series
在某些承诺返回后执行操作