https://kotlinlang.org logo
#coroutines
Title
# coroutines
p

pteale

07/04/2019, 1:47 PM
Whats the equivalent coroutine code in 1.3 for this code..
Copy code
return async {
            val newConsent = consent.copy(createdAt = getConsent()?.createdAt)
            val uniqueId = getUniqueId().await()

            val consentFromApi = userServiceApi.consentApiClient.putConsent(uniqueId, newConsent).await()

            localStorageService.setConsent(consentFromApi)
        }
Which should return a Deferred<Unit>
g

gildor

07/04/2019, 2:32 PM
This also should work on 1.3 (are you talking about coroutines or Kotlin? Anyway, should work on both)
Also, why do you need Deferred<Unit> instead of just Job? Or you want to cache result?
p

pteale

07/04/2019, 2:37 PM
I’m updating some old legacy code and just making it work with 1.3 for now, but ill check out Job if you think that’s more suitable
g

gildor

07/04/2019, 2:37 PM
Depends on case, but usually if you don't want to get result you should use
launch
instead of
async
p

pteale

07/04/2019, 2:38 PM
Thanks
g

gildor

07/04/2019, 2:39 PM
And general approach in coroutines is to use suspend function that returns result (even if this result is Unit) instead of returning Deffered/Job
☝️ 2
d

Dico

07/04/2019, 2:54 PM
GlobalScope.async
3 Views