suspend fun RedisKey.hget(member: String) = suspendCoroutineThis<String> { client.hget(key, member, toVertxHandler()) }
I have two separate questions about corroutines:
--------------------
Something like this is the preferred way to use with vertx? Or there is someother easier way to do this?
suspend fun RedisKey.hget(member: String) = suspendCoroutineThis<String> { client.hget(key, member, toVertxHandler()) }
inline suspend fun <T> suspendCoroutineThis(crossinline block: Continuation<T>.() -> Unit): T = suspendCoroutine { it.block() }
fun <T> Continuation<T>.toVertxHandler(): Handler<AsyncResult<T>> = object : Handler<AsyncResult<T>> {
override fun handle(event: AsyncResult<T>) {
if (event.succeeded()) {
resume(event.result())
} else {
resumeWithException(event.cause())
}
}
}
--------------------
In order to do parallel executions, this is the intended way of doing this? (for direct suspend functions)
// How to do parallelization?
//override fun initAsync() = async {
// val promises = arrayListOf<Promise<Long>>()
// for (missionId in missions.missionIds) {
// promises += sumsKey.hincrbyAsync(missionId, 0L)
// promises += countsKey.hincrbyAsync(missionId, 0L)
// }
// await(promises)
// Unit
//}
override fun initAsync() = async<Unit> {
val promises = arrayListOf<Promise<Unit>>()
for (missionId in missions.missionIds) {
promises += async<Unit> { sumsKey.hincrby(missionId, 0L) }
promises += async<Unit> { countsKey.hincrby(missionId, 0L) }
}
await(promises)
}