Hi folks, ```override suspend fun acceptCode(code...
# coroutines
g
Hi folks,
Copy code
override suspend fun acceptCode(code: String, account: SolidAccount): Unit = coroutineScope {
    val codeVerifierDeferred = async { sessionStorage.retrieveCodeVerifier(account) }
    val urlDeferred = async { sessionStorage.retrieveUserUrl(account) }

    authRequest(codeVerifierDeferred, urlDeferred, code, account)
}
Copy code
override suspend fun acceptCode(code: String, account: SolidAccount): Unit = coroutineScope {
    val codeVerifierDeferred = async { sessionStorage.retrieveCodeVerifier(account) }
    val urlDeferred = async { sessionStorage.retrieveUserUrl(account) }

    authRequest(codeVerifierDeferred.await(), urlDeferred.await(), code, account)
}
Is there a real difference between these two? Thanks in advance !
m
The first code block will immediately call
authRequest
, the second block will call authRequest after both
sessionStorage.retrieveCodeVerifier
and
sessionStorage.retrieveUserUrl
have completed execution. If you are familiar with Java,
Future
is an analogue to
Deferred
(noting that
Future.get
will block while
Deferred.async
suspends).
g
Although im not familiar with Future of java i get what u mean. I pretty much get the differences but im curious about the performance. in my case inside my fun i have this
Copy code
<http://webClient.post|webClient.post>().apply {
    val codeVerifier = codeVerifierDeferred.await()
    bodyValue(TokenRequestBody(code, codeVerifier).asUrlBody())
    val opUrl = urlDeferred.await()
    val dpopHeader = generateDPoPHeader(<http://HttpMethod.POST|HttpMethod.POST>, OpUrl(opUrl))
    header("DPoP", dpopHeader)
    header("content-type", "application/x-www-form-urlencoded")
    uri(opUrl.toTokenEndpoint()).exchangeToFlow {
        flowOf(it)
    }.collect {
My guess is that it would make a difference if was really awaiting much later in the fun?
m
For this specific case, it does not look like it would matter much. However, there are definitely cases where you would want to pass the deferred reference instead of the completed value instead.
g
Thanks for the pointers !