Moisoni Ioan
Moisoni Ioan

Reputation: 1425

Convert Kotlin `suspend fun` to Java

I'm trying to convert a library from Kotlin to Java but this method put a lid on me:

private suspend fun queryPurchases() {
        val inappResult: PurchasesResult = mBillingClient.queryPurchasesAsync(BillingClient.SkuType.INAPP)
        processPurchases(inappResult.purchasesList, isRestore = true)
        val subsResult: PurchasesResult = mBillingClient.queryPurchasesAsync(BillingClient.SkuType.SUBS)
        processPurchases(subsResult.purchasesList, isRestore = true)
    }

How to approach a suspend fun in Java??

Upvotes: 0

Views: 865

Answers (2)

dnault
dnault

Reputation: 8899

Closest thing in Java is a probably a CompletableFuture or a Reactive Stream.

Project Reactor from the Spring ecosystem is a popular Reactive Streams implementation.

Whatever technology you use, porting in a way that preserves the concurrency of the original code is probably not going to be straightforward.

Upvotes: 2

broot
broot

Reputation: 28362

Suspendable functions are a feature added by Kotlin and exclusive to it. You can't easily create or even invoke suspend functions in Java.

You could just ignore the fact that the original function was suspendable and treat it as a regular function, but if the library used coroutines extensively, it could be hard to port it to Java.

Upvotes: 4

Related Questions