nodejs 的async的BUG
let async = require('async');
let util = require('../../util/utils');
test();
function test() {
let task ={time:util.getLocalTime(),
id:0};
async.mapSeries([1,2,3],function (num, callback) {
task.id = num;
if(num>2){
callback(null,{id:task.id,info:task});
}else{
callback(null,{id:task.id,info:task});
}
},function (err, results) {
results.forEach(function (re) {
console.log(re);
})
})
}
//打印的结果是:
//{ id: 1, info: { time: '2017-07-21 14:22:34', id: 3 } }
//{ id: 2, info: { time: '2017-07-21 14:22:34', id: 3 } }
//{ id: 3, info: { time: '2017-07-21 14:22:34', id: 3 } }
//因为我们把task定义在async外面,所以每一次返回的task是最后的task值。要规避此bug,我们直接把task的赋值在async里面就可以了
浙公网安备 33010602011771号