我试图在所有循环迭代完成后才返回回调函数。我没有在谷歌找到类似的案例。
runProfile: function(env_id,profileData, callback){
console.log('****envid: ' + env_id);
console.log('****data: ' + JSON.stringify(profileData[0].title));
for(let i=0,validations = profileData[0].validations; i<validations.length; i++){
console.log('$$$$$$ run validation ' + i +' ' + validations[i].command);
var self = this;
(function(index){
self.runCommand(env_id,validations[index].command, function(res){
if(res.code===0){
validations[index].actual=res.data.trim();
validations[index].status = (validations[index].expected===validations[index].actual) ? "PASSED" : "FAILED";
}
});
})(i);
console.log('finished index: ' + i);
}
callback(profileData[0]);
//
}