Nodejs著名的流程控制库
# Async.js
[](doc.xuehai.net)
Async is a utility module which provides straight-forward, powerful functions
for working with asynchronous JavaScript. Although originally designed for
use with [Node.js](doc.xuehai.net), it can also be used directly in the
browser. Also supports [component](doc.xuehai.net).
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](doc.xuehai.net).
Alternatively, you can install using Node Package Manager (`npm`):
npm install async
__Development:__ [async.js](doc.xuehai.net.
VB 6.0 内从 部使用 MSXML 的步骤为: ①打开...xDoc.async = False If xDoc.Load("http://www....当为 true(默认值) 文件下载完毕就把控制返回给...
jBPM-jPDL 学习笔记——流程设计与控制 作者: 发布...三种节点都可以定义异步处理方式(async 属性): 异步...定义文档是可以直接被 ProcessDefinition 类载入使用的...
--- SND_ASYNC 播放 WAV 文件, 然后将控制立即转移回你的应用程序中, 而不管... 可以使用如下的语句: --- Dim ReturnValue As Long --- ReturnSoundValue =...
async valid_for=(online_logfiles,primary_role) ...按照上面的步骤再切换回来,也是 OK 的 v$database ...用主库得到控制文件启到到 nomount 然后 alter ...
第2章 使用DTD规范XML文档 第3章 结构化程序设计语句... 第3章 使用CSS...表7-4 Document 对象的特有属性 属性 async docType documentEl ement nameSpace...
应用程序中都拥有资源文件,这些文件定义使用应用程序...对话框:对话框的资源定义包括样式标志(控制对话框... As Long Public Const SND_ASYNC = &H1& ...
解决方案 函数式编程之高阶函数高阶函数: 使用函数作为输入参数或作为返回值的...(EventProxy) Promise/Defered模式(Q模块和when模块) 流程控制库(Async和step) ...
控制总线 . 1 数据总线用来传输数据信息,数据总线是...串行通信分为两种方式:异步通信(ASYNC)与同步通信(...逻辑地址:通常程序中使用的地址,也叫虚拟地址,包含...
('MyWav' HInstance Snd_ASync or Snd_Memory or ...资源文件的使用步骤为: 1.编写rc脚本文本用记事本...2.资源文件不但可以在标准图形界面下使用还可在控制...
('MyWav' HInstance Snd_ASync or Snd_Memory or ...资源文件的使用步骤为: 1.编写 rc 脚本文本 用...2.资源文件不但可以在标准图形界面下使用还可在控制...

我要评论