firstOfOrNull

suspend fun <T> firstOfOrNull(vararg deferreds: suspend () -> T): T?

Executes the given suspend producers in parallel and returns the first successfully produced value, or null if none of them produce a value.

Behaviour:

  • All deferreds are started concurrently.

  • Failures are ignored except CancellationException, which is rethrown.

  • As soon as the first value is emitted, upstream flows are cancelled.

Notes:

  • If every producer fails (without throwing CancellationException) or none emits a value, this returns null.

Return

the first successfully produced value, or null if none produced a value

Parameters

T

the type of the produced value

deferreds

suspend producers that are started in parallel

Throws

if the coroutine scope is cancelled or any producer throws it