Spiga
Posts - 175, Articles - 14, Comments - 2601
Cnblogs
Dashboard
Login
Home
Contact
Gallery
RSS
Cat in dotNET
支持链式调用的异步调用框架Async.Operation
2009-06-30 18:05 by Cat Chen, 269 visits,
收藏
,
编辑
Async
=
{}
;
Async.Operation
=
function
(options)
{
options
=
options
||
{}
;
var
callbackQueue
=
[];
var
chain
=
(options.chain
&&
options.chain
===
true
)
?
true
:
false
;
var
started
=
false
;
var
innerChain
=
null
;
this
.result
=
undefined;
this
.state
=
"
running
"
;
this
.completed
=
false
;
this
.yield
=
function
(result)
{
var
self
=
this
;
if
(
!
chain)
{
self.result
=
result;
self.state
=
"
completed
"
;
self.completed
=
true
;
}
else
{
started
=
true
;
self.result
=
result;
self.state
=
"
chain running
"
;
self.completed
=
false
;
}
setTimeout(
function
()
{
if
(
!
innerChain)
{
while
(callbackQueue.length
>
0
)
{
var
callback
=
callbackQueue.shift();
if
(chain)
{
callbackResult
=
callback(self.result);
self.result
=
callbackResult;
if
(callbackResult
&&
callbackResult
instanceof
Async.Operation)
{
innerChain
=
Async.chain();
while
(callbackQueue.length
>
0
)
{
innerChain.next(callbackQueue.shift());
}
innerChain.next(
function
(result)
{
self.result
=
result;
self.state
=
"
completed
"
;
self.completed
=
true
;
return
result;
}
);
callbackResult.addCallback(
function
(result)
{
self.result
=
result;
innerChain.go(result);
}
);
}
}
else
{
callback(self.result);
}
}
if
(
!
innerChain)
{
self.state
=
"
completed
"
;
self.completed
=
true
;
}
}
else
{
while
(callbackQueue.length
>
0
)
{
innerChain.next(callbackQueue.shift());
}
innerChain.next(
function
(result)
{
self.result
=
result;
self.state
=
"
completed
"
;
self.completed
=
true
;
return
result;
}
);
}
}
,
1
);
return
this
;
}
;
this
.go
=
function
(initialArgument)
{
return
this
.yield(initialArgument);
}
this
.addCallback
=
function
(callback)
{
callbackQueue.push(callback);
if
(
this
.completed
||
(chain
&&
started))
{
this
.yield(
this
.result);
}
return
this
;
}
;
this
.next
=
function
(nextFunction)
{
return
this
.addCallback(nextFunction);
}
;
}
;
Async.chain
=
function
(firstFunction)
{
var
chain
=
new
Async.Operation(
{ chain:
true
}
);
if
(firstFunction)
{
chain.next(firstFunction);
}
return
chain;
}
;
Async.go
=
function
(initialArgument)
{
return
Async.chain().go(initialArgument);
}
绿色通道:
好文要顶
关注我
收藏该文
与我联系
Add your comment
10 条回复
1687845
#1楼
普通男孩
2009-10-19 16:21
你好,我是一个js菜鸟.我是这么调用的...
...
var chain = Async.go(0);
chain
.next(function(){setTimeout("alert(1)",3000)})
.next(function(){setTimeout("alert(2)",3000)})
.next(function(){setTimeout("alert(3)",3000)});
...
可是 1, 2, 3 是一块弹出来的 并不是每隔3秒弹一次...
这是怎么回事啊...
回复
引用
查看
#2楼
[
楼主
]
Cat Chen
2009-10-19 16:29
在next里面的异步函数,必须是返回Async.Operation对象的,才会按照阻塞的方式执行。例如:
chain.next(function(){
var operation = new Async.Operation();
setTimeout(function(){ operation.yield("hello"); }, 3000);
return operation;
})
接着你可以再next一下来接收:
chain.next(function(result){
alert(result); // should be "hello"
});
回复
引用
查看
#3楼
普通男孩
2009-10-20 10:12
这回可以了.十分感谢啊,呵呵.不过...还有个问题...
http://www.cnblogs.com/cathsfz/archive/2009/07/01/1514983.html
上面这个地址有个延时函数,我是这么写的...
...
var chain = Async.chain();
alert(typeof chain.wait);//output function
for(var i = 0; i < 5; i++){
chain
.wait(1000)
.next(function() {
var operation = new Async.Operation();
setTimeout(function() {
alert(1);
operation.yield("rst");
}, 2000);
return operation;
})
}
...
很奇怪的是 我在第二行alert的时候可以打印 function, 但是后面就出错了.
我用的是火狐,它的控制台里面报"chain.wait(1000) is undefined",这是怎么回事啊,还有我这么调用对吗?
回复
引用
查看
#4楼
[
楼主
]
Cat Chen
2009-10-20 11:18
@
普通男孩
不好意思,是我手误,在wait里面忘记写return了,所以wait().next()后面的next会出错。你可以自行在wait函数里补上return。
回复
引用
查看
#5楼
普通男孩
2009-10-20 15:21
已经可以了,多谢.
很不错的框架,你的博客我收藏了,以后就跟你混了,呵呵.
回复
引用
查看
#6楼
[
楼主
]
Cat Chen
2009-10-20 15:40
@
普通男孩
:)
回复
引用
查看
#7楼
普通男孩
2009-10-23 13:35
哦 对了.
在wait(1000) 后面我加了 .yield() 才继续执行.不加好像不行...
还有...
if (chain) {
callbackResult = callback(self.result);
...
这个callbackResult 为什么是全局的啊....我加了个var好像也可以...
回复
引用
查看
#8楼
[
楼主
]
Cat Chen
2009-10-23 22:45
@
普通男孩
第一个问题我现在比较难确定,因为我手上的版本已经和写文章时的版本有一定的差别了,而现在的版本fix了一些bug。我懒得手动维护文章里面的代码了,考虑将来直接把新版本发布到一个公开的SVN吧,你有任何建议吗?
第二个问题,其实这个没有var的东西,在找不到更大的scope中有var过的前提下,就相当于本scope内var了。这里确实是我漏写var了,更严谨的写法应该是有var的。
回复
引用
查看
#9楼
普通男孩
2009-11-02 09:21
这两天不舒服也没上来看,不好意思...好没问题,希望你的正式版尽快出来,呵呵.
"第二个问题,其实这个没有var的东西,在找不到更大的scope中有var过的前提下,就相当于本scope内var了。这里确实是我漏写var了,更严谨的写法应该是有var的。"
...
function a()
{
c = 1;
}
function b()
{
alert(c);
}
...
先执行了a方法后,再执行b方法,c就有值了...貌似如果不写var就表示它是全局(window)的对象
回复
引用
查看
#10楼
[
楼主
]
Cat Chen
2009-11-02 14:01
@普通男孩
确实,在没有var的时候,自动把scope放大到window。
回复
引用
查看
注册用户登录后才能发表评论,请
登录
或
注册
,
返回博客园首页
。
首页
博问
闪存
新闻
园子
招聘
知识库
最新IT新闻
:
·
Chrome将给老机带来更快的3D绘图性能
·
在线支付创业公司Stripe获红杉资本等1800万美元的投资,公司估值达1亿美元
·
创新工场孵化公司磊友科技今天正式推出首款大型手机HTML5网页游戏《黎明帝国》
·
霍金的伟大与不幸
·
Linux为什么成功?因为它的失败是免费的!
»
更多新闻...
最新知识库文章
:
·
高级编程语言的发展历程
·
如何学习一门新的编程语言?
·
学习不同编程语言的重要性
·
为什么我喜欢富于表达性的编程语言
·
计算机专业的女生为什么要学编程
»
更多知识库文章...
China-pub 2011秋季教材巡展
China-Pub 计算机绝版图书按需印刷服务
About
我的链接
我的标识
我的简历与作品
@CatChen
Cat in Chinese
Cat in English
Cat on Web Tech
订阅
版权许可
This work is licensed under a
Creative Commons License
.
昵称:
Cat Chen
园龄:
5年3个月
粉丝:
131
关注:
0
随笔分类
Rss
*Book Recommendation*(3)
Rss
*Comprehensive Understanding*(9)
Rss
*Most Practical*(9)
Rss
*Netcasts*(2)
Rss
*Random Clippings*(10)
Rss
*Resources*(8)
Rss
.NET Framework(14)
Rss
AJAX(46)
Rss
ASP.NET(68)
Rss
ASP.NET AJAX(24)
Rss
ASP.NET Futures(8)
Rss
C#(7)
Rss
CSS(14)
Rss
IE(2)
Rss
iPhone(8)
Rss
JavaScript(55)
Rss
LINQ(1)
Rss
Linq to Sql(2)
Rss
Linq to Xml(1)
Rss
Mac(1)
Rss
Node.js(1)
Rss
Rails(4)
Rss
Ruby(4)
Rss
Silverlight(7)
Rss
SQL(4)
Rss
Web(113)
Rss
WF(1)
Rss
Windows(1)
Rss
WPF(1)
Rss
XNA(1)
随笔档案
2012年1月(2)
2011年12月(3)
2011年8月(1)
2011年7月(1)
2011年5月(1)
2010年12月(2)
2010年11月(5)
2010年7月(3)
2010年5月(1)
2010年3月(1)
2010年2月(3)
2010年1月(1)
更多...
2009年12月(4)
2009年11月(5)
2009年10月(3)
2009年9月(1)
2009年8月(2)
2009年7月(3)
2009年6月(4)
2009年5月(4)
2009年4月(1)
2009年3月(2)
2008年12月(3)
2008年11月(2)
2008年10月(2)
2008年9月(2)
2008年4月(2)
2008年3月(5)
2008年2月(1)
2008年1月(7)
2007年12月(6)
2007年11月(4)
2007年10月(4)
2007年9月(1)
2007年8月(2)
2007年7月(3)
2007年6月(4)
2007年5月(4)
2007年4月(1)
2007年3月(5)
2007年2月(7)
2007年1月(3)
2006年12月(13)
2006年11月(11)
2006年10月(30)
推荐排行榜
阅读排行榜
我的链接
Rss
Cat in Chinese
Rss
Cat in English
我的标识
我的简历与作品