async 中的whilst问题
没有循环,只是按顺序走了一边,直接输出了,我想知道错在哪里,请帮我看一下,谢谢
var async =require('async'),
count = 0;
list = [{name:'jack',age:20}, {name:'Lucy',age:18}];
async.whilst(
function () {return count < list.length;},
function (callback) {
console.log(count);
callback(list[count].age += 1);
count ++;
},
function(err){
console.log(err);
}
);
console.log(list);
console.log(count);
8 回复
楼主琢磨琢磨,看这是不是你想要的结果
async.whilst(
function() {
return count < list.length;
},
function(callback) {
console.log('count: ');
console.log(count);
if (count < list.length) list[count].age += 1
count++;
callback();
},
function(err) {
console.log('err:');
console.log(err);
}
);
callback(list[count].age += 1); 这个回调传了 最后一个function function(err){ console.log(err); } 这个err 就是 list[count].age += 1 所以你会打印出 21
var async =require('async');
var count = 0;
var list = [
{name:'jack',age:20},
{name:'Lucy',age:18}
];
async.whilst(
function breaker () {
return count < list.length;
},
function iterator (callback) {
console.log(count);
callback(null, list[count++].age += 1);
},
function(err){
console.log(err);
}
);
console.log(count);
console.log(list);