diff --git a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/FirstOf.kt b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/FirstOf.kt index d16557afc9..baa8ccb36e 100644 --- a/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/FirstOf.kt +++ b/tgbotapi.core/src/commonMain/kotlin/dev/inmo/tgbotapi/utils/FirstOf.kt @@ -1,5 +1,6 @@ package dev.inmo.tgbotapi.utils +import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.merge @@ -53,5 +54,16 @@ suspend fun firstOfOrNull( suspend fun firstOf( vararg deferreds: suspend () -> T ): T { - return firstOfOrNull(*deferreds) ?: error("Unable to get result of deferreds") + val resultFlow = deferreds.map { + flow { + runCatching { + it() + }.onSuccess { + emit(it) + }.onFailure { + if (it is CancellationException) throw it + } + } + }.merge() + return resultFlow.first() }