I have a question, I’m trying to replace promises ...
# coroutines
b
I have a question, I’m trying to replace promises with coroutines but I can’t figure out a way to create a
Deferred<Int>
and then call it and complete it later by another function (Will add an example in a 🧵)
Copy code
fun main(args: Array<String>) {
    val hashMap = HashMap<String, Deferred<Int>>()

    GlobalScope.launch {
        delay(2000)
        println("done waiting")
        requireNotNull(hashMap["test"]).asCompletableFuture().complete(5)
    }

    runBlocking {
        val asyncThing = async {
            getNumberFive()
        }

        hashMap["test"] = asyncThing

        println(asyncThing.await())
    }
}
I guess basically what I want to do is be able to create deferrable and complete it later in another part of the code, is this a bad use case of coroutines and should I just keep using promises? 🤔
b
@stojan thank you!!
s
What about starting async lazily?
var asyncThing = async(start=CoroutineStart.LAZY) { ... }