function* getStockPrice(stock) {
while(true) {
yield Math.random()*100
}
}
let priceGenerator = getStockPrice("IBM")
let limitPrice = 15
let price = 100
while(price > limitPrice) {
price = priceGenerator.next().value
console.log(`the generator return ${price}`)
}
console.log(`buying at ${price}`)
当随机生成的价格低于15的时候,停止运行,如果未低于15,就执行一次getStockPrice函数获取新的随机价格