I have written this function to load data from db, change it asynchronously and return the new data. It appears to work fine, but since I am new to koltin, I wanted to ask if this code is ok by industry standards.
override fun update(resourceCommand: UpdateResourceCommand): List<Resource> = runBlocking {
val resources = resourceCommand.resources.map {
async {
val resource = load(it)
resource.isProtected = it.isProtected
resource
}
}.awaitAll()
return@runBlocking resources
}
Thank you in advance
CodePudding user response:
The question is kind of subjective, but my take on it is that is not really idiomatic to create a variable to only return it after. I believe your code can be made more compact like
override fun update(resourceCommand: UpdateResourceCommand): List<Resource> = runBlocking {
resourceCommand.resources.map {
async {
load(it).apply {
isProtected = it.isProtected
}
}
}.awaitAll()
}
Whether it's better or more to industry standards I don't know and is subjective. But that's how I would do it.