# Async.js
Async is a utility module which provides straight-forward, powerful functions
for working with asynchronous JavaScript. Although originally designed for
use with [node.js](http://nodejs.org), it can also be used directly in the
browser. Also supports [component](https://github.com/component/component).
Async provides around 20 functions that include the usual 'functional'
suspects (map, reduce, filter, each…) as well as some common patterns
for asynchronous control flow (parallel, series, waterfall…). All these
functions assume you follow the node.js convention of providing a single
callback as the last argument of your async function.
## Quick Examples
```javascript
async.map(['file1','file2','file3'], fs.stat, function(err, results){
// results is now an array of stats for each file
});
async.filter(['file1','file2','file3'], fs.exists, function(results){
// results now equals an array of the existing files
});
async.parallel([
function(){ ... },
function(){ ... }
], callback);
async.series([
function(){ ... },
function(){ ... }
]);
```
There are many more functions available so take a look at the docs below for a
full list. This module aims to be comprehensive, so if you feel anything is
missing please create a GitHub issue for it.
## Common Pitfalls
### Binding a context to an iterator
This section is really about bind, not about async. If you are wondering how to
make async execute your iterators in a given context, or are confused as to why
a method of another library isn't working as an iterator, study this example:
```js
// Here is a simple object with an (unnecessarily roundabout) squaring method
var AsyncSquaringLibrary = {
squareExponent: 2,
square: function(number, callback){
var result = Math.pow(number, this.squareExponent);
setTimeout(function(){
callback(null, result);
}, 200);
}
};
async.map([1, 2, 3], AsyncSquaringLibrary.square, function(err, result){
// result is [NaN, NaN, NaN]
// This fails because the `this.squareExponent` expression in the square
// function is not evaluated in the context of AsyncSquaringLibrary, and is
// therefore undefined.
});
async.map([1, 2, 3], AsyncSquaringLibrary.square.bind(AsyncSquaringLibrary), function(err, result){
// result is [1, 4, 9]
// With the help of bind we can attach a context to the iterator before
// passing it to async. Now the square function will be executed in its
// 'home' AsyncSquaringLibrary context and the value of `this.squareExponent`
// will be as expected.
});
```
## Download
The source is available for download from
[GitHub](http://github.com/caolan/async).
Alternatively, you can install using Node Package Manager (npm):
npm install async
__Development:__ [async.js](https://github.com/caolan/async/raw/master/lib/async.js) - 29.6kb Uncompressed
## In the Browser
So far it's been tested in IE6, IE7, IE8, FF3.6 and Chrome 5. Usage:
```html
<script type="text/javascript" src="async.js"></script>
<script type="text/javascript">
async.map(data, asyncProcess, function(err, results){
alert(results);
});
</script>
```
## Documentation
### Collections
* [each](#each)
* [map](#map)
* [filter](#filter)
* [reject](#reject)
* [reduce](#reduce)
* [detect](#detect)
* [sortBy](#sortBy)
* [some](#some)
* [every](#every)
* [concat](#concat)
### Control Flow
* [series](#series)
* [parallel](#parallel)
* [whilst](#whilst)
* [doWhilst](#doWhilst)
* [until](#until)
* [doUntil](#doUntil)
* [forever](#forever)
* [waterfall](#waterfall)
* [compose](#compose)
* [applyEach](#applyEach)
* [queue](#queue)
* [cargo](#cargo)
* [auto](#auto)
* [iterator](#iterator)
* [apply](#apply)
* [nextTick](#nextTick)
* [times](#times)
* [timesSeries](#timesSeries)
### Utils
* [memoize](#memoize)
* [unmemoize](#unmemoize)
* [log](#log)
* [dir](#dir)
* [noConflict](#noConflict)
## Collections
<a name="forEach" />
<a name="each" />
### each(arr, iterator, callback)
Applies an iterator function to each item in an array, in parallel.
The iterator is called with an item from the list and a callback for when it
has finished. If the iterator passes an error to this callback, the main
callback for the each function is immediately called with the error.
Note, that since this function applies the iterator to each item in parallel
there is no guarantee that the iterator functions will complete in order.
__Arguments__
* 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.
__Example__
```js
// assuming openFiles is an array of file names and saveFile is a function
// to save the modified contents of that file:
async.each(openFiles, saveFile, function(err){
// if any of the saves produced an error, err would equal that error
});
```
---------------------------------------
<a name="forEachSeries" />
<a name="eachSeries" />
### eachSeries(arr, iterator, callback)
The same as each only the iterator is applied to each item in the array in
series. The next iterator is only called once the current one has completed
processing. This means the iterator functions will complete in order.
---------------------------------------
<a name="forEachLimit" />
<a name="eachLimit" />
### eachLimit(arr, limit, iterator, callback)
The same as each only no more than "limit" iterators will be simultaneously
running at any time.
Note that the items are not processed in batches, so there is no guarantee that
the first "limit" iterator functions will complete before any others are
started.
__Arguments__
* arr - An array to iterate over.
* limit - The maximum number of iterators to run at any time.
* 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.
__Example__
```js
// Assume documents is an array of JSON objects and requestApi is a
// function that interacts with a rate-limited REST api.
async.eachLimit(documents, 20, requestApi, function(err){
// if any of the saves produced an error, err would equal that error
});
```
---------------------------------------
<a name="map" />
### map(arr, iterator, callback)
Produces a new array of values by mapping each value in the given array through
the iterator function. The iterator is called with an item from the array and a
callback for when it has finished processing. The callback takes 2 arguments,
an error and the transformed item from the array. If the iterator passes an
error to this callback, the main callback for the map function is immediately
called with the error.
Note, that since this function applies the iterator to each item in parallel
there is no guarantee that the iterator functions will complete in order, however
the results array will be in the same order as the original array.
__Arguments__
* 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, transformed) which must be called once
it has completed with an error (which can be null) and a transformed item.
* callback(err, results) - A callback which is called after all the iterator
functions have finished, or an error has occurred. Results is an array of the
transformed items from the original array.
__Example__
```js
async.map(['file1','file2','file3'], fs.stat, functi
程序员Chino的日记
- 粉丝: 3726
- 资源: 5万+
最新资源
- Lumerical FDTD仿真,逆向设计的片上功率分束器项目,项目工程+脚本代码+1.7w字报告,都很完整
- 光伏电池MPPT仿真模型 1.光伏电池参考一篇文献搭建的 2.在温度25 光照1000的时候光伏电池最大功率10kw 3.仿真在1秒和2秒的时候光照增加200、减小200,可以看到在扰动观察法点作用
- 饼干分包sw20可编辑全套技术资料100%好用.zip
- 变频与移相混合控制的全桥LLC谐振变器 有参考文献(看下图) 控制与移相控制组成的混合式控制全桥LLC谐振变器仿真(PFM+PSM混合控制) 输出电压闭环控制稳定在360V,软开关,宽范围,可实现调频
- 插座耐久试验台step全套技术资料100%好用.zip
- 2024中国大数据产业白皮书.pdf
- 多晶介电击穿相场模拟comsol电树枝 采用comsol相场模拟陶瓷介电击穿过程 晶粒与晶界具有不同的击穿场强,由于晶界的阻挡作用,击穿强度增加 并且晶界在电场作用下出现介电常数降低现象 晶界面
- 文字生成视频-可灵1.6
- DSP芯片程序读取 DSP28德州仪器28系列DSP反汇编,定点器件和浮点器件均支持,能够根据out、hex或bin文件建立可以编译的CCS汇编语言工程,并且编译后可生成二进制完全相同的bin文件,方
- 文字生成视频-可灵1.6
- 储能集装箱sw18可编辑全套技术资料100%好用.zip
- 关键词:微电网;储能系统;一致性算法;充放电分布式协同优化; ## 非完整复现,控制部分未做; 主题:在微电网系统中,储能系统(ESSs)常被用来支持频率控制 由于可再生能源发电的间歇性和负荷需求
- 光伏+混合储能三相并网超级电容能量管理 1、光伏MPPT最大功率跟踪 2、混合储能采用低通滤波器进行功率分配,可有效抑制功率波动, 3、对超级电容的soc进行能量管理,soc较高时多放电,较低时少放
- 阅后即焚平台系统源码 PHP版本.zip
- 在线Base64加解密HTML源码.zip
- EKF扩展卡尔曼滤波算法做电池SOC估计,在Simulink环境下对电池进行建模,包括: 1.电池模型 2.电池容量校正与温度补偿 3.电流效率 采用m脚本编写EKF扩展卡尔曼滤波算法,在Simuli
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈