Open evantianx opened 7 years ago
async function getStockPrice(name, cancelToken) {
await.cancelToken = cancelToken
let symbol = await getStockSymbol(name, cancelToken)
let price = await getSymbolPrice(stmbol, cancelToken)
return price
}
let { token, cancel } = cancelToken.source()
getStockPrice("Johnson and Johnson", token)
.then(price => console.log(price))
// 随时终止异步操作
cancel()
async function* getStocks() {
let reader = new AsyncFileReader('stock.txt')
try {
while (!reader.eof) {
let line = await reader.readline()
yield JSON.parse(line)
}
}
finally {
await reader.close()
}
}
使用示意图:
看视频的总结笔记📒