首页 > 其他 > 详细

用Generator简化异步操作代码

时间:2015-06-18 09:21:57      阅读:205      评论:0      收藏:0      [点我收藏+]

 

有点像Promise的变形, 目前还没想到更优雅的写法。

总觉得Generator有点像线程,有中断有唤醒,Generator应该可以模拟多线程时间片的效果。

 

function async(x, _genObj) {
setTimeout(function() {
_genObj.next(x + x);
}, 100);
}

function* testGen(aa) {
console.log(‘第一步‘);
var re = yield async(aa,genObj);
console.log(‘第二步‘, re);
re = yield async(re,genObj);
console.log(‘第三步‘, re);
re = yield async(re,genObj);
console.log(‘第四步‘, re);
}
var genObj = testGen(2);
genObj.next();

用Generator简化异步操作代码

原文:http://www.cnblogs.com/felix-grey/p/4584917.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!