Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

554
Views
Why the *Async naming convention for functions returning Deferreds?

Intellij has an inspection (i.e. lint check) that tells you functions that return Deferred should be named something ending in Async.

enter image description here

Naming conventions like these make sense to me in dynamically typed languages. But Kotlin has such a nice type-checker and ecosystem of tooling, so why rely on the convention?

Especially because Kotlin coroutines bake structured concurrency in, the function will probably also take a CoroutineScope parameter, which would serve the same visual cue at the call site:

suspend fun doStuff() = coroutineScope {
  doStuffAsync(this /* CoroutineScope */).await()
  //...
}

As a side note, I understand the inspection's message that you'd rarely want a function that returns a Deferred instead of a suspend function. That's not my question. My question assumes that you know what you're doing and you want a Deferred.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

To begin with, a function should almost never return a Deferred that comes from an async block. It is up to the caller to wrap some unit of work into an async while doing other work in the foreground, then await on the async result before returning, and wrap all that code in a coroutineScope.

The intended source of Deferred instances is the adaptation layer between Kotlin coroutines and 3rd-party async APIs. For such calls it does make sense to put Async in the name and even some Java APIs follow this convention. For example, you may have a

fun fetchOrder(id: String): Deferred<Order>

and use it as

val orderCancelled = fetchOrder(orderId).isCancelled

This code is type-safe and type-correct, it doesn't cause any compiler errors. It looks like it's fetching an order from a remote system and then checking the order status (whether it's cancelled), but what it's actually doing is getting a Deferred<Order> and checking whether the Deferred is cancelled. Because your function name is missing Async, this kind of error is hard to spot.

Yes, you can also request the IDE to give you the return type, but it may take a while before you even suspect what's going on.

over 4 years ago · Santiago Trujillo Report

0

Deferred doesn't return the actual value but is "a light-weight non-blocking future that represents a promise to provide a result later".

As you cannot see this from method name, "we name such functions with the "...Async" suffix to highlight the fact that they only start asynchronous computation and one needs to use the resulting deferred value to get the result".

So the reader of your code can instantly see that your method is not returning the actual value but additionally has to call await() on it.


To your second point about the suspend function:

Usually it's other way round, the xyzAsync() function calls the suspend xyz() function.

As functions returning Deferred can be called from anywhere, not only from suspend functions.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!