Async.js 过滤数组无效
Async.js filtering an array not working
我正在尝试使用异步的过滤方法,但我没有得到我期望的结果
async.filter([1, 3, 5], function (item, done) {
done(item > 1);
}, function (results) {
console.log(results);
});
async.filter([1, 2, 3, 4, 5, 6], function(item, callback) {
if (item > 3) {
callback(true);
} else {
callback(false);
}
},
function (result) {
console.log("result: " + result);
});
输出是
true
result: true
而不是 2 个过滤数组,我错过了什么?
我认为你应该使用一些不同的语法,就像这里指定的那样:async#filter
回调的结果应该是第二个参数(不是第一个):callback(null, true)
例如:
async.filter([1, 3, 5], function (item, done) {
done(null, item > 1);
}, function (err, results) {
console.log(results);
});
async.filter([1, 2, 3, 4, 5, 6], function(item, callback) {
if (item > 3) {
callback(null, true);
} else {
callback(false);
}
},
function (err, result) {
console.log("result: " + result);
});
我正在尝试使用异步的过滤方法,但我没有得到我期望的结果
async.filter([1, 3, 5], function (item, done) {
done(item > 1);
}, function (results) {
console.log(results);
});
async.filter([1, 2, 3, 4, 5, 6], function(item, callback) {
if (item > 3) {
callback(true);
} else {
callback(false);
}
},
function (result) {
console.log("result: " + result);
});
输出是
true
result: true
而不是 2 个过滤数组,我错过了什么?
我认为你应该使用一些不同的语法,就像这里指定的那样:async#filter
回调的结果应该是第二个参数(不是第一个):callback(null, true)
例如:
async.filter([1, 3, 5], function (item, done) {
done(null, item > 1);
}, function (err, results) {
console.log(results);
});
async.filter([1, 2, 3, 4, 5, 6], function(item, callback) {
if (item > 3) {
callback(null, true);
} else {
callback(false);
}
},
function (err, result) {
console.log("result: " + result);
});