ES6之异步流程的前世今生(下)
generator/co
function* foo() {
yield 1;
yield 2;
yield 3;
return console.log(4)
}
var it = foo();
it.next();
it.next();
it.next();
it.next(); //4function *doSomething() {
console.log(‘start’)
yield
console.log(‘finish’)
}
var func1 = doSomething();
func1.next();
func1.next();
function* getStorckPrice(stock) {
while (true) {
yield Math.random() * 100;
}
}
var priceGenerator = getStockPrice(‘IBM’);
var limitPrice = 15;
var price = 100;
while (price > limitPrice) {
price = priceGenerator.next().value;
console.log( `this generator return ${price}` );
}
console.log( `buying at ${price}` );
generator 原理
async/await
async/await 原理
后记
相关知识参考资料
最后更新于