說command_arr.length === 2ES5,如何在一個循環中使用的承諾
當我運行下面的函數device_execute_command。執行的消息順序是
finish one command
finish one command
has latest state?
has latest state?
我想到的是:
finish one command
has latest state?
finish one command
has latest state?
代碼
var device_execute_command = function(command_arr) {
// This is the main loop
var i = 0;
for(i=0; i < command_arr.length; i++) {
var command = command_arr[i];
var command_id = command.command_id;
device_run_single_command(command).then(function(command){
console.log();
console.log("finish one command");
console.log(command);
return is_device_has_latest_state(command);
}).then(function(command_with_condi){
console.log();
console.log("has latest state?");
console.log(command_with_condi);
});
}
}