async简单使用
node的异步io虽然好用,但是控制异步流程确实一个比较麻烦的事情,比如在爬虫中控制并发数量,避免并发过大导致网站宕机或被加入黑名单。因此需要一个工具来控制并发,这个工具可以自己写或者使用async(官方文档点击这里)。代码基于node 8.x,如版本过低可能会出现错误。
说明
async本身有七十多个方法,这里只说明几个比较常用的简单函数用法,想进一步学习可参考文档。总的来说分为两大类。
一、第一个参数为函数集合,也就是遍历执行集合中的函数。
1.顺序执行 series(tasks , function(err,res){ })
tasks为函数数组,数组中的每一项都为待执行函数。
a.下面是一个最简单示例,待执行函数为非异步
const asyncx = require( 'async' ); let tasks = [];
for( let i = 0 ; i < 10 ; i++ )
{
tasks.push(
function( callback ){
//dosomething
console.log( i );
//本函数用来通知async本次任务完成情况,并把结果带出去。
callback( null , i ); //第一个参数为异常参数,如果传入一个error( 比如new Error('error') ),并发结束,调用series里的回调。
}
)
}
asyncx.series( tasks , function(err , res ){
if( err )
console.log( err );
console.log( res );
} )
运行结果
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAVgAAADeCAYAAAB48hgxAAAbsElEQVR4Ae3dfUxb56EG8GeTjRK8JmbV3G72zR2Z4ki1pUEbQSrcakEqtLtQDd+19DLIBvwB0QCtpmuSabANb0rSFXLV9LbmD0CrSRZaXRMt7KYwCa4WUAPK5kQyV8JR66rB6+aqNW1qmCBSr47Nh42B+BwO2Bw/kSrs43Pej997/Pj161P7S/v27fsC/EcBClCAArILfFn2ElkgBShAAQqEBRiwPBEoQAEKbJEAA3aLYFksBShAAQYszwEKUIACWySgElWu1oSjv34Whwy7ocYCPp26grNto/hHSFQp3JkCFKBAWgiImMF+BUW/qMChhWt46UcvwvbydcxlP43nm/enBRQ7SQEKUECsQOIBa34Cj2ffwfU3rmB6Bpgb7ccFzx3sNVvwiNhauT8FKECBNBBIPGDzv4H7Qx/gultQ2YUD9bX4ofk+IEOHbH0aSLGLFKAABUQKiFuDhQoP1h7FM8Vm7AlN4apjBA81HcLebAB+kTVzdwpQgAI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//ab2lFtnhp951atvDhqKtUj+C0G90dMl+CtpVlL8Er9O+X9u3bd6/LB5Lc9cUnq2YewakBnDnZG/uWa3XrMo/g+LlKoLsWcn1YnHe8C3UZvfwugtXWvE+BTQvELmUp7bsIdkDAbnoEWQAFKECBpAiEryJISs2slAIUoIDCBRiwCh9gdo8CFEieAAM2efasmQIUULgAA1bhA8zuUYACyRNgwCbPnjVTgAIKF2DAKnyA2T0KUCB5Av8PRx/56XWvCiIAAAAASUVORK5CYII=" alt="" />
b.待执行函数为异步
如果待处理的函数也是异步函数可将callback参数传入到异步函数中,在真正结束的时候调用callback。
const asyncx = require( 'async' ); //模块命名为asyncx避免和es7中的async/await冲突 let tasks = [];
for( let i = 0 ; i < 10 ; i++ )
{
tasks.push(
function( callback ){
setTimeout( function(){
console.log(i);
callback( null , 1 ); //在这里整个处理才是真正完成,然后调用callback通知async本任务结束
} , 2000 );
}
)
}
asyncx.series( tasks , function(err , res ){
if( err )
console.log( err );
console.log( res );
} )
如果想要在任务中使用es7的async/await 可将待处理代码放在一个闭包中(直接在function前加async会报错),下面示例。
const asyncx = require( 'async' ); //模块命名为asyncx避免和es7中的async/await冲突 let tasks = [];
for( let i = 0 ; i < 10 ; i++ )
{
tasks.push(
function( callback ){
(async function(){
let sum = await doSomething( i );
callback( null , sum );
})();
}
)
} async function doSomething( i ){
return new Promise( function( resolve , reject ){
setTimeout(function(){
console.log( i );
resolve( i );
} , 1000 );
} );
} asyncx.series( tasks , function(err , res ){
if( err )
console.log( err );
console.log( res );
} )
2.并发执行 parallel( tasks , function(err,res){} )
参数如上,不限制并发
3.并发限制执行parallelLimit( tasks , num , function(err,res){} )
参数同上,num为最大并发数量
二、第一个参数为非函数集合。
比如
async.map(['file1','file2','file3'], function(item,callback){
//dosomething
callback( null , 'done' );
}, function(err, results) {
// results is now an array of stats for each file
});
第二个参数为一个异步函数,要能接受两个参数item(前面集合中的一项),callback 通知async任务完成
还有其他的函数用法都是类似只是具体作用不一样。可参考官方文档说明。
async简单使用的更多相关文章
- es6,async简单总结
1.简单来讲就是把函数变为异步操作的 async function demo() { let result = Math.random(); console.log(result); } 2.asyn ...
- 从0到1学习node(七)之express搭建简易论坛
我们需要搭建的这个简易的论坛主要的功能有:注册.登录.发布主题.回复主题.下面我们来一步步地讲解这个系统是如何实现的. 总索引: http://www.xiabingbao.com/node/2017 ...
- 对Promise的一些深入了解
1.介绍promise和模仿Promise.all和Promise.race promise的设计主要是解决回调地狱(接收结果用回调函数来处理,但必须传入回调函数)的问题,由一层层嵌套回调函数改为由t ...
- 【转】【C#】C# 5.0 新特性——Async和Await使异步编程更简单
一.引言 在之前的C#基础知识系列文章中只介绍了从C#1.0到C#4.0中主要的特性,然而.NET 4.5 的推出,对于C#又有了新特性的增加--就是C#5.0中async和await两个关键字,这两 ...
- @Async的简单用法总结
前言: 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的:但是在处理与第三方系统交互的时 候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring 3 ...
- 异步async/await简单应用与探究
感谢Marco CAO指出的两点错误,已做出修改与补充 异步函数(async/await)简单应用 .NET Framework4.5提供了针对异步函数语法糖,简化了编写异步函数的复杂度. 下面通过一 ...
- 转:[你必须知道的异步编程]C# 5.0 新特性——Async和Await使异步编程更简单
本专题概要: 引言 同步代码存在的问题 传统的异步编程改善程序的响应 C# 5.0 提供的async和await使异步编程更简单 async和await关键字剖析 小结 一.引言 在之前的C#基础知 ...
- [你必须知道的异步编程]C# 5.0 新特性——Async和Await使异步编程更简单
本专题概要: 引言 同步代码存在的问题 传统的异步编程改善程序的响应 C# 5.0 提供的async和await使异步编程更简单 async和await关键字剖析 小结 一.引言 在之前的C#基础知 ...
- HTML 5 <script> async 属性简单设置代码异步执行
HTML5中 script标签支持脚本的异步执行async.脚本将会异步运行: <script type="text/javascript" src="demo_a ...
随机推荐
- [Hadoop源码系列] FairScheduler分配申请和分配container的过程
1.如何申请资源 1.1 如何启动AM并申请资源 1.1.1 如何启动AM val yarnClient = YarnClient.createYarnClient setupCredentials( ...
- Gym 100952 D. Time to go back(杨辉三角形)
D - Time to go back Gym - 100952D http://codeforces.com/gym/100952/problem/D D. Time to go back time ...
- hadoop 50070 无法访问问题解决汇总
遇到这个其实不难解决! 解决办法1: [root@djt002 hadoop]# vi /etc/selinux/config 改为 SELINUX=disabled 解决办法2: 查看你的$HADO ...
- FreeRTOS——内存管理
1. 标准malloc() 和 free() 库函数的缺陷: 1)在小型的嵌入式系统中,可能不可用. 2)具体实现相对较大,占用较多宝贵的代码空间. 3)通常不具备线程安全性. 4)具有不确定性,每次 ...
- Python面向对象编程(三)
封装 1.为什么要封装? 封装就是要把数据属性和方法的具体实现细节隐藏起来,只提供一个接口.封装可以不用关心对象是如何构建的 2.封装包括数据的封装和函数的封装,数据的封装是为了保护隐私,函数的封装是 ...
- 微信小程序开发问答《五十四》同步请求授权 & 用户拒绝授权,重新调起授权 ... ...
1.同步请求授权 需求分析: 1.在小程序首次打开的时候,我需要同时请求获取多个权限,由用户逐一授权. (['scope.userInfo','scope.userLocation','scope.a ...
- 运行Jmeter.bat出错:Not able to find java executor or version. Please check your installation. errorlevel=2
下载JMeter. 解压后运行Jmeter.bat竟然报错了. 解决办法整理: 方法1: 1.检查JDK环境变量配置: ①系统变量→新增JAVA_HOME. 变量值填写jdk的安装目录(本人是 E:\ ...
- NLP —— 图模型(零):EM算法简述及简单示例(三硬币模型)
最近接触了pLSA模型,该模型需要使用期望最大化(Expectation Maximization)算法求解. 本文简述了以下内容: 为什么需要EM算法 EM算法的推导与流程 EM算法的收敛性定理 使 ...
- [javascript] visible - 待写
摘要 jquery 有个筛选器 visible , 一般用于选择 可见元素 $('p:visible') 就是选择可见的 p 元素. 但发现有时候不可用.!!
- [js] post 方式打开新窗口
一.前因 一般我们是用 window.open(url,name,params); 打开新窗口, url 会携带一些参数, 但存在参数过多,引发url 过长截断,无法打开正确窗口, 所以我们需要使用 ...