async和awat的API设计问题
来源:12-6 案例:仿官方框架实现 async

昨日重现1596341
2020-05-26
kotlin的async和await的API设计感觉不是很合理,为什么不直接内部调用await代码如下,这样就不用为开发者暴露DeferredCoroutine接口出去使用会更简单。
suspend fun asyncDirect(coroutineContext: CoroutineContext = EmptyCoroutineContext, block: suspend () -> T): T {
val completion = Deferred(newCoroutineContext(coroutineContext))
block.startCoroutine(completion)
return completion.await()
}
然后在DeferredCoroutine的await方法不可以直接调用doOnComplete吗,感觉有一些冗余重复代码。
suspend fun await(): T = suspendCoroutine { resultContinuation ->
doOnComplete {
resultContinuation.resumeWith(it)
}
}
是不是我遗漏了什么知识点导致这块理解产生问题。
写回答
1回答
-
bennyhuo
2020-05-26
有考虑怎么解决 启动模式 协程作用域 之类的问题吗? 看你的写法应该是没有00
相似问题