Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

318
Visualizações
When to use Kotlin suspend keyword?
fun startAsyncFunc() {
  launch {
    asyncFunc1()
    asyncFunc2()
  }
}

fun asyncFunc1() { ... }
suspend fun asyncFunc2() { ... }

I can finish the work without suspend and it even makes test easier (it can be tested without adding runBlocking.

My questions:

  1. asyncFunc1 vs asyncFunc2, which is better and why?
  2. If asyncFunc2 is better, should I always use suspend whenever a function will be ran in the coroutines?

Update

In the recent releases of Kotlin Coroutines, I notice if a method doesn't contain any coroutines code(like launch, async, etc), the compiler complains This inspection reports a suspend modifier as redundant if no other suspend functions are called inside. So I assume that suspend should be only used when it's a must.

Update2

An advice from Google

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You should only declare your function suspend if it needs to. I would say that, when in doubt, if the compiler does not force you, don't use suspend.

Most of the time, if you have a good reason for your function to be suspending, it means it's doing something that probably requires you to call suspending functions like withContext anyway, or it might be a callback based function that you expose as suspending via suspendCoroutine/suspendCancellableCoroutine. Either way calling those functions would force you to declare your own function suspend.

Note that declaring a function suspend does not enable your callers to do anything more than they could when your function was not suspending. If anything, you're limiting the use of your function.

One possible exception to the rule "don't use it unless forced to" would be if you're defining this function as an open method (for instance in an interface) and you expect that some implementations/overrides will need to call suspending functions themselves.

over 4 years ago · Santiago Trujillo Relatório

0

suspend keyword means coroutine can be suspended for later execution.

Having said that, you should consciously use it them for coroutines that will get suspended (e.q. your asyncFunc2() made a HTTP call and is awaiting response to process it)

So.

  1. Use suspend for functions that will be delayed in some way (Awaiting some computations, api response etc.)
  2. suspend fun can be run from coroutine only. So, if it gets suspended, it will block the coroutine. Take out the suspend keyword, but run it in coroutine and it will have the same effect. However, if you run this function from outside the coroutine, it will block the thread it was running on.

When testing coroutines, you should always invoke runBlocking. If you don't, a coroutine that gets suspended may not complete, resulting in a failed test.

over 4 years ago · Santiago Trujillo Relatório

0

Use suspend keyword for:

  • functions that will be delayed in some way (awaiting some computations, Network Requests )

    • whenever the function calls other suspend functions
    • function that call withContext() – withContext() is a suspend function that comes from the Coroutine Library

The rule of the thumb is don’t mark a function suspend unless you are forced to

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda