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+kTVzdwpQgAIKFxAXs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tnhp951atvDhqKtUj+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 ...
随机推荐
-
python基础-RE正则表达式
re 正则表示式 正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C 编写 ...
-
Java Web学习过程的思维导图
今天找文件,无意中翻到老师前段时间总结的知识点.觉得应该有点用处,所以分享给大家. 第一次在博客园发表,如有错误,还请指正.
-
c# 服务端
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
-
Extjs中GridPanel的各个属性与方法
1.Ext.grid.GridPanel 主要配置项: store:表格的数据集 columns:表格列模式的配置数组,可自动创建ColumnModel列模式 autoExpandColumn:自动充 ...
-
一种非常巧妙的读取串口数据的方法--C#
读取不完就一直等待,读完了就立刻走,之前都是设置一个溢出时间,不管是不是早就读取完了都要在这等着,有一定的时间浪费. 注意,用之前要设置好SerialPort类的TimeOut属性:
-
ES 7 async/await Promise
如何添加SSL证书实现https请求 https://blog.csdn.net/lupengfei1009/article/details/76828190/ ES 7 async/awai ...
-
#WEB安全基础 : HTML/CSS | 0x1初识CSS
"我受够这些难看的网页了,我怎么才能让它变得好看点?"你说. 我答道:"看来你得学点CSS了" 学习这些东西只有一个原则,就是用你的脑袋想,用你的眼睛看,用的你 ...
-
golang rpc介绍
rpc包提供了通过网络或其他I/O连接对一个对象的导出方法的访问.服务端注册一个对象,使它作为一个服务被暴露,服务的名字是该对象的类型名.注册之后,对象的导出方法就可以被远程访问.服务端可以注册多个不 ...
-
Mac/Ubuntu下的数据建模工具PDMan,替代PowerDesigner
PowerDesigner我使用过用Wine在Linux和Mac下用,但总有些缺陷,用Navicat却发觉没有Linux版本的: 一般关心的问题主要有如下: 1.数据库的关系设计图. 2.导出数据库脚 ...
-
springboot2.1.3.RELEASE+jsp笔记war部署tomcat
springboot+jsp <packaging>war</packaging> <parent> <groupId>org.springframew ...