Hi, How I do I properly structure this code? I ha...
# coroutines
a
Hi, How I do I properly structure this code? I have the following methods
Copy code
suspend fun getData1(): DataModel1 { ... }
suspend fun getData1(): DataModel1 { ... }
suspend fun getData1(): DataModel1 { ... }

fun processData(
  data1 : DataModel1,
  data2 : DataModel2,
  data3 : DataModel3,
): Boolean { ... }
I have a suspend method which uses all the above methods like this currently.
Copy code
suspend fun usageMethod() {
  coroutineScope {
    val deferred = awaitAll(
      async { 
        getData1()
      },
      async { 
        getData1()
      },
      async { 
        getData1()
      },
    )
    processData(deferred[0], deferred[1], deferred[2])
  }
}
This works as expected, but I am unable to return the
Boolean
result of the
processData()
from the
usageMethod()
. Note: I am using async to execute all the data fetching methods in parallel.
c
whats wrong with
Copy code
suspend fun usageMethod(): Boolean {
    return coroutineScope {
        val deferred = awaitAll(
            async {
                getData1()
            },
            async {
                getData2()
            },
            async {
                getData3()
            },
        )
        processData(deferred[0], deferred[1], deferred[2])
    }
}
?
a
Yes, that seems to work 😃. Thank You thank you color
👍 1