https://kotlinlang.org logo
#coroutines
Title
# coroutines
d

dai

03/08/2018, 9:58 AM
I may have missed it but is there anyway to fire and forget an async call within a suspend function?
j

Jonathan

03/08/2018, 10:00 AM
Hmm... yes like from any other function...
Copy code
fun foo() {
  launch { doSomethingUsefull() }
}
This doesn't wait for the
doSomethingUsefull
to complete and don't care if it does'nt work
d

dai

03/08/2018, 10:12 AM
Thanks for the reply, yeah i need to do it with in the suspend
thats my problem
j

Jonathan

03/08/2018, 10:13 AM
Copy code
suspend fun foo() {
  launch { doSomethingUsefull() }
}
why is it a problem ?
d

dai

03/08/2018, 10:33 AM
Yeah I don’t think what I need is possible with some coroutines without it being a bit of a hack
so going to rework things a bit
thanks for the reply
j

Jonathan

03/08/2018, 10:34 AM
Could you give a code example?
b

bdawg.io

03/08/2018, 6:50 PM
The block passed to
launch
is a suspending function. The
Job
that is returned does not block the rest of your procedures until you invoke
suspend fun join()
on the job. Maybe I’m misunderstanding your example?
Copy code
suspend fun foo() {
    val job = launch {
        delay(2000)
        println("job is finished")
    }
    println("foo continues")
    job.join()
    println("foo is finished")
}
would result in
Copy code
foo continues
job is finished
foo is finished
3 Views