Existuje způsob, jak implementovat <T>(x: Promise<AsyncIterableIterator<T>>): AsyncIterableIterator<T> v TypeScriptu?
Takže dostanu Promise<AsyncIterableIterator<T>> a potřebuji obyčejný AsyncIterableIterator<T>, jak mohu rozbalit AsyncIterableIterator<T> z pod promise?
Odpověď č. 1
Ano, ve skutečnosti je to docela jednoduché. Chceme sami implementovat rozhraní AsyncIterableIterator a při každém volání next nejdříve rozbalíme Promise.
function unwrapAsyncIterableIteratorPromise (input) {
return {
next (value) {
return input.then((iterator) => iterator.next(value))
},
[Symbol.asyncIterator] () {
return this
}
}
}
Je to také na npm spolu s typovými definicemi pro TypeScript:
https://www.npmjs.com/package/unwrap-async-iterable-iterator-promise
