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];});
总结,真是个神库
- async笔记(一)——collections
- underscore学习笔记—集合函数/collections(一)
- async笔记(二)——process
- Commons Collections学习笔记(一)
- async-http-client开源库学习笔记(一)
- Simple-Java—Collections(一)Java高效计数器
- underscore学习笔记—集合函数/collections(二)
- ES6 —(async 函数)
- Collections 浅析(一)
- Android菜鸟的成长笔记(13)——异步任务(Async Task)
- Android菜鸟的成长笔记(13)——异步任务(Async Task)
- AngularJS2 学习笔记——Async Data Binding
- Java笔记 - 集合(Collections)
- java笔记(十一):Collections
- [Python标准库]collections——容器数据类型[一]
- async.js 学习教程(一)
- android-async-http源码解读(一)
- iOS学习笔记--浅拷贝和深拷贝——拷贝集合类(Copying Collections)
- 第三章 数据链路层 3.5扩展的以太网
- Yarn框架概述
- 设计模式初探-解释器模式
- CUDA程序优化小记(六)
- 微信5.2 for iPhone 全体验:新增图片墙、语音转文字,可搜索聊天记录,群聊被@会收到提醒
- async笔记(一)——collections
- 全表扫描
- 条件变量:BlockingQueue+CountDownLatch
- CUDA程序优化小记(七)
- LCD Backlight 的分析
- 动态规划——最长连续子序列和+最大子矩阵和
- 自己的第一篇博文
- SQL sever 2008远程连接
- 寒假英语学习初感受