Getting a value with timeout using Kotlin coroutines So, here's a task. A background thread may or may not call all of it's listeners some time after sendRequest() was called (in other words sendRequest() schedules a possible event). The required function must:


Subscribe to those events
Call sendRequest() periodically until the function returns anything
Return the first suitable result (filtering is done in the listener and is omitted in this snippet) or null after a timeout
Unsubscribe from those events regardless of the result


suspend fun findServer(): Server? {
    var listener: ((Server) -&gt; Unit)? = null
    return try {
        withTimeoutOrNull(10000) {
            suspendCancellableCoroutine&lt;Server&gt; { continuation -&gt;
                listener = { server: Server -&gt;
                    continuation.resume(server)
                }.also { discoveryService.addListener(it) }

                launch {
                    while (continuation.isActive) {
                        discoveryService.sendRequest()
                        delay(1000)
                    }
                }
            }
        }
    } finally {
        listener?.let { discoveryService.removeListener(it) }
    }
}


The current implementation seems to work fine, but the nullable listener variable feels kind of clunky and requires using also to keep the listener reference, which in turn breaks type inference and makes me specify listener's parameter type (no big deal, but...). I could use a lateinit var here, but it probably can lead to exceptions when withTimeoutOrNull() stops before the listener was initialized.

Any recommendations on improving this snippet? Or am I, in fact, doing everything totally wrong here?
Think it's pretty good and you can't think of much else. Some tiny bits I can think of:


Make discoveryService parameter.
Do not use launch without specifying CoroutineScope.