'How to use AsyncGenerators with Kotlin/JS?

I'm currently trying to use IPFS with Kotlin/JS, though my problem isn't specific to that. The ipfs.cat() and ipfs.get() functions return an AsyncGenerator and I'm unsure how to iterate over it with Kotlin (I'm not even sure which type would best represent an AsyncIterable in kotlin)

The below code is an minimal version of what I'm trying to do, though I have not tested the code as is below. It fails with a ClassCastException since the for loop is fundamentally wrong, but I don't know what I should replace it with.

File1:

@file:JsModule("ipfs-core")
@file:JsNonModule

import kotlin.js.Promise

@JsName("create")
external fun create(config: Any = definedExternally): Promise<dynamic>

File2:

create().then { ipfs: dynamic ->
    ipfs.id().then { id: dynamic ->
        myId = id.id as String
        println(JSON.stringify(id))
    }
    val result: dynamic = ipfs.cat("bafkreihapp6racx2xf5gwnrgtsr56r37kazui3jvzzmot2nx2t6h6g2oom")
    // result is an AsyncGenerator

    // below fails with ClassCastException
    for (element: dynamic in result){
        println(element)
    }
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source