Hi again! So, I'm using a library that expects me ...
# javascript
b
Hi again! So, I'm using a library that expects me to return an
AsyncIterator
. For instance here's something in JS :
Copy code
myAsyncIterator = async function* () {
        for await (const word of ["one", "two", "three"]) {
          yield { count: word };
        }
      }
Any idea how to write an equivalent in Kotlin?
t
You can check
AsyncIterator
contract in
lib.dom.d.ts
to write effective addapter
b
Thanks! I can't seem to find it there 🤔
but I'm especially wondering how one can write the equivalent of an "function*" (a generator function) in Kotlin
and an "async" one at that
t
I can’t seem to find it there 🤔
Do you use IDEA?
And which compiler do you use?
b
Yes IDEA, and the IR one
t
Screenshot 2022-04-23 at 23.00.37.png
It’s from IDEA typings
b
Very interesting! I wonder why I can't see this but I'll investigate. Thanks a lot!
Very interesting! I wonder why I can’t see this but I’ll investigate
1. Create JS file 2. Write
Symbol.asyncIterator
3.
Cmd
+ click on
asyncIterator
4. PROFIT
👀 1
b
wow! Very cool. Let me see if I can make this work in my case
well, that helped me a lot! I finally managed to make this work. Huge thanks! 🙏
t
Do you have analog of initial code?
b
(sorry for the late reply) You can see how I did it here.
I use a bunch of
dynamic
so it's weakly typed, but that's ok for my use-case
t
You can use wrappers for strict typings
👍 1
s
Idiomatically JS generators (
function*
) relates to
suspend
in Kotlin but I think we (wrappers developers) need some invent some wrappers and strategies to make integration to generators more seamless, example of such integration is
Copy code
public suspend fun <T> kotlin.js.Promise<T>.await(): T { /* compiled code */ }
from
kotlinx.coroutines
b
yeah it kinds of makes sense in the end when you know where to look at 😅 But I think I wouldn't have made it alone without the pointers from Victor. Maybe it's easier if you're a more experienced JS developer than myself.