# Async.js
[![Build Status via Travis CI](https://travis-ci.org/caolan/async.svg?branch=master)](https://travis-ci.org/caolan/async)
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)
* [`eachSeries`](#eachSeries)
* [`eachLimit`](#eachLimit)
* [`map`](#map)
* [`mapSeries`](#mapSeries)
* [`mapLimit`](#mapLimit)
* [`filter`](#filter)
* [`filterSeries`](#filterSeries)
* [`reject`](#reject)
* [`rejectSeries`](#rejectSeries)
* [`reduce`](#reduce)
* [`reduceRight`](#reduceRight)
* [`detect`](#detect)
* [`detectSeries`](#detectSeries)
* [`sortBy`](#sortBy)
* [`some`](#some)
* [`every`](#every)
* [`concat`](#concat)
* [`concatSeries`](#concatSeries)
### Control Flow
* [`series`](#seriestasks-callback)
* [`parallel`](#parallel)
* [`parallelLimit`](#parallellimittasks-limit-callback)
* [`whilst`](#whilst)
* [`doWhilst`](#doWhilst)
* [`until`](#until)
* [`doUntil`](#doUntil)
* [`forever`](#forever)
* [`waterfall`](#waterfall)
* [`compose`](#compose)
* [`seq`](#seq)
* [`applyEach`](#applyEach)
* [`applyEachSeries`](#applyEachSeries)
* [`queue`](#queue)
* [`priorityQueue`](#priorityQueue)
* [`cargo`](#cargo)
* [`auto`](#auto)
* [`retry`](#retry)
* [`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 the function `iterator` to each item in `arr`, 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 its `callback`, the main
`callback` (for the `each` function) is immediately called with the error.
Note, that since this function applies `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 `arr`.
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 when all `iterator` functions
have finished, or an error occurs.
__Examples__
```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
});
```
```js
// assuming openFiles is an array of file names
async.each(openFiles, function( file, callback) {
// Perform operation on file here.
console.log('Processing file ' + file);
if( file.length > 32 ) {
console.log('This file name is too long');
callback('File name too long');
} else {
// Do work to process file here
console.log('File processed');
callback();
}
}, function(err){
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});
```
---------------------------------------
<a name="forEachSeries" />
<a name="eachSeries" />
### eachSeries(arr, iterator, callback)
The same as [`each`](#each), only `iterator` is applied to each item in `arr` in
series. The next `iterator` is only called once the current one has completed.
This means the `iterator` functions will complete in order.
---------------------------------------
<a name="forEachLimit" />
<a name="eachLimit" />
### eachLimit(arr, limit, iterator, callback)
The same as [`each`](#each), only no more than `limit` `iterator`s will be simultaneously
running at any time.
Note that the items in `arr` 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 `iterator`s to run at any time.
* `iterator(item, callback)` - A function to apply to each item in `arr`.
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 when all `iterator` functions
have finished, or an error occurs.
__Example__
```js
// Assume documents is an array of JSON objects and requestApi is a
// function that interacts
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
cesium-point cloud转换 (1317个子文件)
marked.1 2KB
ctio.3ctype 8KB
LICENSE.BSD 1KB
benchmark-native.c 613B
Cakefile 624B
CHANGELOG 2KB
ChangeLog 413B
.classpath 1KB
strip-json-comments.cmd 149B
esvalidate.cmd 147B
esparse.cmd 141B
uuid.cmd 133B
marked.cmd 131B
jsdoc.cmd 125B
ipaddr.coffee 11KB
ipaddr.test.coffee 9KB
jsl.conf 6KB
tocdoc.css 124KB
foundation.min.css 84KB
built-editor.css 15KB
jsdoc-default.css 5KB
qunit.css 5KB
Animation.css 4KB
BaseLayerPicker.css 3KB
CesiumInspector.css 3KB
InfoBox.css 3KB
NavigationHelpButton.css 3KB
shared.css 2KB
Timeline.css 2KB
SceneModePicker.css 2KB
Geocoder.css 2KB
prettify-tomorrow.css 2KB
lighter.css 2KB
InfoBoxDescription.css 2KB
parse.css 2KB
Viewer.css 2KB
prettify-jsdoc.css 1KB
CesiumWidget.css 1KB
lighterShared.css 1KB
lighter.css 1KB
style.css 958B
lighter.css 696B
widgets.css 687B
prettify.css 675B
SelectionIndicator.css 648B
lighter.css 621B
lighter.css 463B
lighter.css 319B
PerformanceWatchdog.css 315B
lighter.css 280B
FullscreenButton.css 135B
VRButton.css 127B
.dir-locals.el 178B
railsTemplate.js.erb 469B
.eslintrc 2KB
esparse 284B
esvalidate 290B
conf.json.EXAMPLE 342B
info-loading.gif 723B
.gitignore 22B
.gitignore 16B
.gitignore 9B
.gitignore 6B
.gitmodules 283B
.gitmodules 207B
.gitmodules 207B
bench.gnu 6KB
esprima.js.html 337KB
index.html 92KB
index.html 18KB
parse.html 6KB
index.html 6KB
rewrite.html 6KB
minify.html 6KB
autocomplete.html 6KB
index.html 6KB
validate.html 6KB
compare.html 6KB
functiontrace.html 5KB
collector.html 5KB
precedence.html 5KB
benchmarks.html 5KB
highlight.html 5KB
compat.html 4KB
index.html 4KB
module.html 4KB
coverage.html 4KB
transpile.html 3KB
taffy-test.html 2KB
index.html 2KB
index.html 1KB
dist-min.html 1KB
dist-concat.html 1KB
test.html 340B
CesiumPointCloudGenerator.jar 17.03MB
sqlite-jdbc-3.16.1.jar 5.88MB
3dcitydb-impexp.jar 2.55MB
gt-epsg-hsql-10.5.jar 1.9MB
jai_core-1.1.3.jar 1.81MB
gt-main-10.5.jar 1.58MB
共 1317 条
- 1
- 2
- 3
- 4
- 5
- 6
- 14
资源评论
- 星逐月2019-03-22恶心!资源地址:https://github.com/tum-gis/cesium-point-cloud-generator
- blackwarror2018-12-10这明明是开源的一个项目,非要被拿来赚钱,作者太恶心了 !
- 最美的风景在路上2019-01-11我也想说这句话.....
果丹皮1
- 粉丝: 39
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功