Kotlinx.coroutines: JavaScript support

Created on 1 Mar 2017  路  11Comments  路  Source: Kotlin/kotlinx.coroutines

Any plan about to include JavaScript specifications into the library?

It could be great to make this library as a reference.

import org.w3c.fetch.Response
import kotlin.coroutines.experimental.*
import kotlin.js.Promise

external fun fetch(url: String): Promise<Response> = definedExternally

suspend fun <T> Promise<T>.await(): T = suspendCoroutine {
    then(it::resume).catch(it::resumeWithException)
}

fun <T> async(block: suspend () -> T) = Promise<T> { resolve, reject ->
        block.startCoroutine(completion = object : Continuation<T> {
            override val context: CoroutineContext = EmptyCoroutineContext

            override fun resume(value: T) {
                resolve(value)
            }

            override fun resumeWithException(exception: Throwable) {
                reject(exception)
            }
        })
    }
}


fun asyncTest() = async {
    val value = fetch("./test.txt").then(Response::text).await()

    println(value)
}
enhancement

Most helpful comment

Yes. There is a plan to have a subset of kotlix-coroutines-core module available in JS and to provide additional utility functions for JS Promise.

All 11 comments

Yes. There is a plan to have a subset of kotlix-coroutines-core module available in JS and to provide additional utility functions for JS Promise.

Any ideas on when this might be implemented?

No specific timeline yet.

I would really like this support... any update to the
timeline?

Stay tuned

What does 芦Stay tuned禄 means?

It would be nice to give some piece of information. We have to plan how we develop our stuffs. Knowing an estimated date for a first version can help us taking decisions.

It's the oldest opened issue.

"Stay tuned" means we plan to do it. No specific date. We plan to do it as a multi-platform project from the very start, so we are currently waiting for 1.2 release with MP support.

So with the 1.2 being quite close, we can expect it within reasonable time.

I'm waiting this multiplatform version too, for a multiplatform reactive stream library I'm writing (using a lot of kotlinx-coroutines-reactive stuff).

@elizarov if you want, I can fork this repository or you can create a branch from "develop" so I can start writing multiplatform version with 1.2 RC (only if it can be useful) ?

Released in version 0.21

Sorry to open this again... but I just wanted to say:
You guys are truly awesome! Thank you for all you effort!
Im truly amazed by the vision you are bringing to life here.
Will support you from Vienna by participating in regular Kotlin-Meetups and of course by using your products <3

Was this page helpful?
0 / 5 - 0 ratings