So I get Promise<AsyncIterableIterator<T>>
and I need plain AsyncIterableIterator<T>
how can I unwrap the AsyncIterableIterator<T>
from under the promise?
Asked
Active
Viewed 863 times
2

Trident D'Gao
- 18,973
- 19
- 95
- 159
-
Sure, why not? Have you tried it? – Bergi Apr 06 '18 at 18:08
-
1I'd guess `for await (const e of (await x)) yield e` should do. – Bergi Apr 06 '18 at 18:09
-
i tried to implement interfaces bare-hands, but your async `for` is the spot on – Trident D'Gao Apr 06 '18 at 18:24
1 Answers
1
Yes, it's actually quite simple. We want to implement the AsyncIterableIterator
interface ourselves, and for every call to next
we first unwrap the Promise
.
function unwrapAsyncIterableIteratorPromise (input) {
return {
next (value) {
return input.then((iterator) => iterator.next(value))
},
[Symbol.asyncIterator] () {
return this
}
}
}
It's also on npm together with TypeScript typings:
https://www.npmjs.com/package/unwrap-async-iterable-iterator-promise

Linus Unnebäck
- 23,234
- 15
- 74
- 89