sequelize(SQL)等待直到数组已被更新

我有我需要更新的对象列表

router.route('/api2/user/bulk') .put(function (req, res) { if(req.body.users && req.body.fieldKey) { req.body.users.forEach(function (x) { var updateVariable = {}; updateVariable[req.body.fieldKey] = _.get(x, req.body.fieldKey); model.user.update(updateVariable, {where: {id: x.id}}); }) } }); 

现在的问题是节点在循环完成之前继续执行。 这意味着我不能确定,如果我在最后添加一个res.status(200).send('ok') ,那么所有更新都会被更新。

我的问题是我如何确保更新已经通过之前,我发送回应给我的客户?

使用asyncfunction。

  // Include the async package // Make sure you add "async" to your package.json async = require("async"); // 1st para in async.each() is the array of items async.each(items, // 2nd param is the function that each item is passed to function(item, callback){ // Call an asynchronous function, often a save() to DB item.someAsyncCall(function (){ // Async call is done, alert via callback callback(); }); }, // 3rd param is the function to call when everything's done function(err){ // All tasks are done now doSomethingOnceAllAreDone(); } ); 

为了更好地理解你可能谷歌的asynchronousJS教程