0
我有一個場景,我需要在下面的系列中進行多個調用。 我正在更新由'loadBaseData'函數返回的主數據對象,然後需要再次調用'grindCoffee'函數對同一數據對象進行額外的修改。expressJs操作相同的數據對象的多個回調
但是數據並沒有抵制,在processResponse中,所有的選項都是空的。
app.post('/makecoffee', loadBaseData, grindCoffee, addWater, brewCoffee, processResponse);
function loadBaseData (req, res, next) {
pgServer.dbSelectData(req.body.username)
.then(function(data) {
req.data = data;
next();
})
.catch(function(err) {
res.send({success:false,message:'error, '+ err});
});
}
function grindCoffee (req,res,next){
var records = req.data;
for (j = 0; j< records.length; j++) {
if (Condition A){
dsServer.grindCoffee(function(res){
if (Condition B){
req.data[i].options = res;
next();
}
});
}
}
next();
}
// addWater and brewCoffee are similar to grindCoffee, it keeps updating the req.data
function processResponse(req,res){
res.send({success:true,data:req.data});
}
編輯: 我想,也許使用像藍鳥承諾可能與此情況有所幫助,但我不知道如何將其轉換爲它。
不叫循環中'下一個()'的呢? – Bergi