https://kotlinlang.org logo
#getting-started
Title
# getting-started
x

xii

02/11/2022, 4:17 PM
i have a question about suspend functions and await (thread)
lets say you have a function
Copy code
suspend fun returnVal(){
  anotherClass.checkSomething()
  return "hello"
}
check something looks like something like this:
Copy code
suspend fun checkSomething(){
  if (asyncFunction()){
    throw Exception()
  } else {
    return
  }
}
and lastly,
Copy code
suspend fun asyncFunction(){
   return anotherAsync.await() == false
}
since there is an await on asyncFunction, its not necessary to explicitly await the checkSomething() function to make sure it will throw when it needs to, before returning "hello", correct?
v

Vampire

02/11/2022, 4:50 PM
You cannot explicitly await
checkSomething()
as it is a
Unit
function.
await()
is a function on
Deferred
. And yes, as you
await
anotherAsync
,
asyncFunction
will not complete until it is finished and thus
checkSomething
will not complete earlier either.
x

xii

02/11/2022, 5:19 PM
as expected, awesome! thank you
i thought i might have to convert to deferred unit or something, haha!
2 Views