Моя функция выглядит вот так-
Код: Выделить всё
suspend fun getPrefix(messageCreateEvent: MessageCreateEvent): String {
val snapshot = db.collection("prefixes")
.document(messageCreateEvent.guildId.get().asString())
.get() //This returns a future
.get() //Retrieves the future's result (Blocks thread; IDE gives warning)
//Return the prefix
return if (snapshot.exists())
snapshot.getString("prefix") ?: DEFAULT_PREFIX
else DEFAULT_PREFIX
}
Первое, что я подумал, — это поискать в kotlinx.coroutine расширения, позволяющие соединить будущее. Хотя расширения существуют, они подходят только для CompletionStatge. Поэтому я решил обернуть будущее в один ()-
Код: Выделить всё
val snapshot = CompleteableFuture.supplyAsync {
db.collection("prefixes")
.document(messageCreateEvent.guildId.get().asString())
.get() // This returns a future
.get() // Get the result
}.await()
Код: Выделить всё
val deferred = CompletableDeferred()
val future = db.collection("prefixes")
.document(messageCreateEvent.guildId.get().asString())
.get()
future.addListener(
Runnable { deferred.complete(future.get()) },
ForkJoinPool.commonPool()
)
val snapshot = deferred.await()
Подробнее здесь: https://stackoverflow.com/questions/633 ... the-thread