/**
* Created by wlh on 16/1/8.
*/
var Promise = require("bluebird");
var fs = Promise.promisifyAll(require("fs"));
var path = require("path");
function myCo(gen) {
var g = gen();
fn();
function fn(result) {
result = g.next(result);
if (result.done) {
//数字转promise
return "finish...."
} else {
if (typeof result.value.then != 'function') {
throw new Error("yield only support promise...");
}
return result.value.then(function (val) {
//进行递归
return fn(val);
})
}
}
}
function *generate() {
// var result1 (= yield) 不是简单的赋值, result的值取决于 g.next(值),next的值将给result1
var result1 = yield fs.readFileAsync(path.join(__dirname, "test.html").toString());
console.info("result1", result1.toString());
var result2 = yield fs.readFileAsync(path.join(__dirname, "server.js").toString());
console.info("result2", result2.toString())
var result3 = yield fs.readFileAsync(path.join(__dirname, "test.js").toString());
console.info("result3", result3.toString());
}
myCo(generate);
转载于:https://my.oschina.net/wanglihui/blog/599689