异步模块async中的filter具体用法
发布于 8 年前 作者 jinghaonode 5387 次浏览 来自 问答

async.filter([1, 5, 3, 7, 2], function(item, callback){

callback(item > 3);

}, function(results){

console.log(results);// [5, 7]

});

如上代码正常应该是返回 [5, 7], 为什么老是返回true呢?

4 回复

QQ截图20160414224600.png 我执行的结果就是[5,7]

最后的返回结果处理,第一个参数是err,第二参数是results 然后处理函数的callback第一个是err,第二个是值

async.filter([‘file1’,‘file2’,‘file3’], function(filePath, callback) { fs.access(filePath, function(err) { callback(null, !err) }); }, function(err, results){ // results now equals an array of the existing files });

https://github.com/caolan/async/tree/master/dist 可能是版本差异,引用上面的 async.js 出现的问题 如果引用//cdn.bootcss.com/async/1.4.0/async.js 则正常返回[5,7]

async.filter([1, 5, 3, 7, 2], function (item, callback) {
    callback(null,item > 3);
}, function (err,results) {
    console.log(results);// [5, 7]
});

按最新api改写成上面的即可正常,感谢楼上两位回答
回到顶部