I may have missed it but is there anyway to fire a...
# coroutines
d
I may have missed it but is there anyway to fire and forget an async call within a suspend function?
j
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
Thanks for the reply, yeah i need to do it with in the suspend
thats my problem
j
Copy code
suspend fun foo() {
  launch { doSomethingUsefull() }
}
why is it a problem ?
d
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
Could you give a code example?
b
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