利用Generator解决异步回调原理

var i = 0;
i++;
function ajax(url){
	return new Promise(function(resolve, reject){
		setTimeout(function(){
			console.log(url);
			resolve({url: url + i++});
		}, 200);
	});
}

function *gen(url){
	var data = yield ajax(url);
	data = yield ajax(data.url);
	data = yield ajax(data.url);
	console.log(data.url);
}

function co(gen){
	var iterator = gen("http://www.baidu.com");
	var v = iterator.next();
	
	function step(v){
		if (!v.done){
			v.value.then(function(data){
				step(iterator.next(data));
			})
		}
	}
	
	step(v);
}

co(gen);
posted @ 2016-05-15 23:49  木的树  阅读(1320)  评论(0编辑  收藏  举报