問題
我很好奇,如果有可能消耗在一個發電機功能全異步/等待現代ES2017內上下文。 (應用程序是一個陣營,本機應用程序)如何使用「等待」發電機功能是ES6?
這是我的代碼,我想打電話給發生器功能:
class ProductViewComponent extends Component {
async componentDidMount() {
const result = await loadProduct(...)
console.log(result) // Gives: *Generator* and not the final result
}
}
功能loadProduct()
從另一個文件導入,定義如下:
export function * loadProduct(id) {
let product = yield select(productByIdSelector(id));
if(!product) {
// ... yield this
// ... yield that
// ... finally:
product = yield call(loadProductFromWeb, id)
}
return product;
}
具體問題:
據我知道我可以使用await
等待Promises的結果。我如何在這種情況下使用生成器函數?
gen = loadProduct();等待gen.next(); –
顯然.next()爲loadProduct-Function中的每個「yield」語句提供了許多結果(在裏面有很多yield-statements,比在問題中顯示的多) – delete
我還沒有得到用例嗎?發生器返回什麼?承諾? –