async笔记(一)——collections

来源:互联网 发布:2016外商直接投资数据 编辑:程序博客网 时间:2024/06/05 04:17

async在node和浏览器里都可以用,API基本没区别

node环境:

var async = require("async");async.xxx();

浏览器环境:

<script type="text/javascript" src="async.js"></script><script type="text/javascript">    async.xxx();</script>

本文总结下各API调用的方法。本文的示例代码在:AsyncExample

each

Array的迭代器方法,很常用

  • arr - An array to iterate over.
  • iterator(item, callback) - A function to apply to each item in the array.The iterator is passed a callback(err) which must be called once it has completed. If no error has occured, the callback should be run without arguments or with an explicit null argument.
  • callback(err) - A callback which is called after all the iterator functions have finished, or an error has occurred.

async.each(["abc", "def", "ghi"], function(item, callback){        console.log(item);    callback(null);// must call once    }, function(err){        if(err){        console.error("error");    }});

eachSeries

和each基本一样,但是顺序执行,API接口和参数都一样

eachLimit

也和each差不多,多出来的limit参数,是限制允许并发执行的任务数

async.eachLimit(["123", "456", "789"], 2, function(item, callback){    console.log(item);    callback();// 必须调用,才能触发下一个任务执行}, function(error){    if(error){        console.error("error: " + error);    }});

map

将一个Array中的元素,按照一定的规则转换,得到一个新的数组(元素个数不变),也比较常用

async.map([1,3,5], function(item, callback){    var transformed = item + 1;    callback(null, transformed);}, function(err, results){    if(err){        console.error("error: " + err);        return;    }    console.log(results);// [2, 4, 6]});

mapSeries和mapLimit

基本差不多,就不多介绍了

filter

用于过滤Array中的元素

Returns a new array of all the values which pass an async truth test. The callback for each iterator call only accepts a single argument of true or false, it does not accept an error argument first! This is in-line with the way node libraries work with truth tests like fs.exists. This operation is performed in parallel, but the results array will be in the same order as the original.

filter的同名函数是select

  • arr - An array to iterate over.
  • iterator(item, callback) - A truth test to apply to each item in the array. The iterator is passed a callback(truthValue) which must be called with a boolean argument once it has completed.
  • callback(results) - A callback which is called after all the iterator functions have finished.

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

filterSeries

类似

reject和rejectSeries

和filter正好相反,filter是保留true的item,而reject是删除true的item

async.reject([4, 7, 88, 11, 36], function(item, callback){    callback(item > 11);}, function(results){    console.log(results);// [4, 7, 11]});

reduce和reduceRight

将一个数组中的元素,归并成一个元素,看看就好了,用得不是很多,可以需要的时候再查

  • arr - An array to iterate over.
  • memo - The initial state of the reduction.
  • iterator(memo, item, callback) - A function applied to each item in the array to produce the next step in the reduction. The iterator is passed a callback(err, reduction) which accepts an optional error as its first argument, and the state of the reduction as the second. If an error is passed to the callback, the reduction is stopped and the main callback is immediately called with the error.
  • callback(err, result) - A callback which is called after all the iterator functions have finished. Result is the reduced value.

async.reduce([3, 2, 1], 0, function(memo, item, callback){    callback(null, memo + item)}, function(err, result){    if(err){        console.error("error: " + err);        return;    }    console.log(result);// 6});

reduceRight差不多,只是Array中元素迭代的顺序是相反的:

async.reduceRight(["!", "ld", "wor"], "hello ", function(memo, item, callback){    callback(null, memo + item)}, function(err, result){    if(err){        console.error("error: " + err);        return;    }    console.log(result);// hello world!});

detect和detectSeries

从数组中找出符合条件的元素

这个API很像filter,参数也都一样,但是只会返回一个结果

async.detect([13, 44, 23], function(item, callback){    callback(item > 37);}, function(result){    console.log(result);// 44});

sortBy

数组元素排序

  • arr - An array to iterate over.
  • iterator(item, callback) - A function to apply to each item in the array. The iterator is passed a callback(err, sortValue) which must be called once it has completed with an error (which can be null) and a value to use as the sort criteria.
  • callback(err, results) - A callback which is called after all the iterator functions have finished, or an error has occurred. Results is the items from the original array sorted by the values returned by the iterator calls.

var person1 = {"name": "aaa", age:79};var person2 = {"name": "bbb", age:23};var person3 = {"name": "ccc", age:54};async.sortBy([person1, person2, person3], function(person, callback){    callback(null, person.age);}, function(err, sorted){        console.log(sorted);});

some

同名函数any。在数组中找至少一个元素,类似于filter和detect。区别在于,filter和detect是返回找到的元素,而some是返回bool

async.some([1, 5, 9], function(item, callback){    callback(item > 10);}, function(result){    console.log(result);// false});

every

同名函数all。跟some相反,如果数组中所有元素都满足条件,则返回true,否则返回false

async.every([1, 21, 23], function(item, callback){    callback(item > 10);    }, function(result){    console.log(result);// false});

concat

对数组中的元素进行迭代操作,形成一个新数组

  • arr - An array to iterate over
  • iterator(item, callback) - A function to apply to each item in the array. The iterator is passed a callback(err, results) which must be called once it has completed with an error (which can be null) and an array of results.
  • callback(err, results) - A callback which is called after all the iterator functions have finished, or an error has occurred. Results is an array containing the concatenated results of the iterator function.

async.concat([1, 2, 3], function(item, callback){    callback(null, [item+1, item+2]);}, function(err, results){    console.log(results);// [2, 3, 3, 4, 4, 5];});

总结,真是个神库 

0 0
原创粉丝点击