async await

279 阅读1分钟
function * read() {
    let content = yield fs.read('./open.text','utf8');
    let body = yield fs.read('./content.text','utf8');
    return body;
}


const it = read()
let {value,done} = it.next();
let {value,done} = it.next(value);
console.log(value)

// 怎样实现才能自动执行呢
// ====== //

myCo(read()).then((data)=>{
	console.log(data) data ==> 上面的body
})
function myCo(it) {
	return newPromise((resolve,reject)=>{
    	function next(nextVal) {
        	let {value,done} = it.next(nextVal);
            done && resolve(value)
            !done && Promise.resolve(value).then((data)=>{
            	next(data)
            })
        }
        next()
    })
}