NODEJS(13)Platform - Async.js
1. Async.js Introduction
It is designed for use with Node.js, but it can also be used directly in the browser.
It support series, parallel, waterfall.
>sudo npm install -g async
There are 3 main parts, Flows, Collections, Utils.
2. Flows
series(tasks, [callback])
We have multiple functions which need to execute, they do not need to talk to each other, just need them execute one by one.
step1(function(err,v1){
step2(function(err,v2){
step3(function(err,v3){
//do something, we can get the err or values v1/v2/v3
}
}
}
———>
var async = require(‘async’)
async.series([step1, step2, step3],
function(err, values){
//err and values including v1/v2/v3
});
Support JSON format
Functions will be executed by turns, if one of the functions came across err, the series will stop and call the callback method with error information immediately, the left functions will not be executed.
Some examples
https://github.com/freewind/async_demo
parallel(tasks, [callback])
all the functions will be executed in parallel, the callback result will include the results in order of the tasks defined, NOT in the order of who executed complete first who be the first.
async.parallel([
function(cb){ t.fire(‘a400’, cb, 400) },
function(cb){ t.fire(‘a200’, cb, 200) },
function(cb){ t.fire(‘a300’, cb, 300) }
], function (err, results) {
log(‘1.1 err: ‘, err); // undefined
log(‘1.1 results: ‘, results); // [‘a400’, ‘a200’, ‘a300’ ]
})
Support JSON format
https://github.com/freewind/async_demo/blob/master/parallel.js
waterfall(tasks, [callback])
execute in turns, the next function need the result from previous function as parameter.
NOT support JSON
async.waterfall([
function(cb) { log(‘1.1.1: }, ‘start’); cb(null, 3),
function(n, cb) { log(‘1.1.2: ', n); t.inc(n,cb); },
function(n, cb) { log(‘1.1.3: ‘, n); t.fire(n*n, cb); }
], function(err, result){
log(‘1.1 err: ‘, err); // null
log(‘1.1 result: ‘ , result); // 16 ——> 3 + 1, 4*4 = 16
});
auto(tasks, [callback])
https://github.com/freewind/async_demo/blob/master/auto.js
Some series, some parallel.
whilst(test, fn, callback)
util(test, fn, callback)
queue
powerful parallel, there is an idea of workers, system will not execute the tasks at one time, it depends on the workers.
var q = async.queue(function(task, callback) {
log(‘worker is processing task: ‘, task.name);
task.run(callbck);
}, 2);
When we use out all the workers, system will call saturated
q.saturated = function(){
log(‘ all workers to be used’);
}
When last tasks is processing, system will call empty
q.empty = function(){
log(‘ no more tasks waiting’);
}
When all tasks are done, system will call drain
q.dran = function(){
log(‘all tasks have been done’);
}
How to Put the Tasks
q.push({name: ‘t1’, run: function(cb) {
log(‘t1 is running, waiting tasks: ‘, q.length());
t.fire(‘t1’, cb, 400);//400ms delay
}}, function (err){
log(‘t1 executed’);
});
https://github.com/freewind/async_demo/blob/master/queue.js
q.push([{name:'t3', run: function(cb){
log('t3 is running, waiting tasks: ', q.length());
t.fire('t3', cb, 300); // 300ms后执行
}},{name:'t4', run: function(cb){
log('t4 is running, waiting tasks: ', q.length());
t.fire('t4', cb, 500); // 500ms后执行
}}], function(err) {
log('t3/4 executed');
});
iterator(tasks)
3. Utils
memoize(fn, [hasher])
something like cache, and we can define the hasher to decide how to generate the cache key.
Image we have a function which is very slow. slow_fn(x, y, callback)
var fn = async.memoize(slow_fn);
That will make fn to be a cache fn.
fn(‘x’,’b’, function(err,result){
console.log(result);
});
fn(‘x’,’b’, function(err,result){
console.log(result);
}); // the cache will hit next time when we call
Change the cache key rules
var fn_hasher = async.memoize(slow_fn, function(x, y){
return x+y;
});
unmemoize(fn) <——> memoize(fn, hasher)
var fn_slow_2 = async.unmemoize(fn);
3. Collections
forEach(arr, iterator(item, callback), callback(err))
all the function in iterator will run in parallel
var arr = [{name:’Jack’, delay:200},
{name:’Mike’, delay:100},
{name:’Freeman’, delay:300}];
async.forEach(arr, function(item,callback){
log(‘1.1 enter:’ + item.name);
setTimeout(function(){
log(‘1.1 handle:’ + item.name);
callback();
},item.delay);
},function(err){
log(‘1.1 err:’+err);
});
If we want them run series mode, we need to use forEachSeries
If we want to make them more complex, some run in parallel, some run series. We can use forEachLimit
async.forEachLimit(arr, 2, function(item, callback){
…snip...
}, function(err){
…snip...
});
every 2 will be a group, they will run in parallel, between groups, they will be series.
https://github.com/freewind/async_demo/blob/master/forEach.js
map(arr, iterator(item, callback), callback(err, results))
The most different part is the results, if you need the results then use map, No care about result, use forEach.
async.map(arr, function(item, callback){
setTimeout(function(){ callback(null, item.name + ‘!!!”);},item.delay);
}, function(err, results){
log(‘1.1 err: ‘, err); log(‘1.1 results:’, results);
});
The same things mapSeries, more example https://github.com/freewind/async_demo/blob/master/map.js
filter(arr, iterator(item, callback(test)),callback(results))
filter all the items >=3
async.filter([1,2,3,4,5], function(item,callback){
setTimeout(function(){ callback(item>=3); },200);
},function(results){
log(‘1.1 results: ‘, results); //[3,4,5]
});
The same for filterSeries https://github.com/freewind/async_demo/blob/master/filter_reject.js
reject(arr, iterator(item, callback(test)),callback(results)) <—> filter
detect(array, iterator(item, callback(test)), callback(result))
Find the first one match the test case. <——>detectSeries
sortBy(array, iterator(item, callback(err, result)), callback(err, results))
sort all the items based on the result, from small to large number.
some/any(arr, iterator(item, callback(test)),callback(result))
Find at least one item in array match the test case
every/all(arr, iterator(item, callback(test)),callback(result))
Find all/every item in array match the test case
concat(arr, iterator(item, callback(err, result)), callback(err, result))
concat all the results together to make a big array
References:
Async
https://github.com/caolan/async
http://blog.fens.me/nodejs-async/
http://freewind.me/blog/20120515/917.html
http://freewind.me/blog/20120517/931.html
http://freewind.me/blog/20120518/932.html
- 浏览: 2500920 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 445NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 302Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 390Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 350Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 434NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 381Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 309Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 221GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 408GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 287GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 283Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 279Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 263Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 252Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 250NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 237Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 534NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 238Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 334Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 332Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
nodejs(node-v16.13.2-x64.msi)——(node-v18.12.1-x64.msi)
nodejs-restful-源码.rar
nodejs-auth-master.rar
nodejs-stream-手册.pdf
Ansible-nodejs-dev-vm.zip,simple node.js使用vagrant virtualbox ansiblesimple node.js开发vm,ansible是一个简单而强大的自动化引擎。它用于帮助配置管理、应用程序部署和任务自动化。
Node.js 是一个跨平台的 JavaScript 运行时环境。 Node.js 使用高效、轻量级的事件驱动、非阻塞 I/O 模型。Node.js 的生态系统是目前最大的开源包管理系统。 Node.js 是一套用来编写高性能网络服务器的 JavaScript ...
# node -v v10.24.1 # npm -v 6.14.12
nodejs-microservice-starter, NodeJS RESTful API Microservice Starter NodeJS RESTful API Microservice v1.2.0这个存储库包含运行 NodeJS rest式 API Microservice启动器的完整配置。 要求NodeJS用于本地开
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
NodeJS-182.3911.9
MariaDB Connector/Node.js(mariadb-connector-nodejs-2.5.4.tar.gz)
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
微信小游戏-斗地主,包含nodejs-服务器-nodejs-server-wechat-landLordGame
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
NodeJS-133.434.zip
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装