.resumeWith(Result.failure(...)) 和 .resumeWithException 之间的区别
Difference between .resumeWith(Result.failure(...)) and .resumeWithException
我可能缺少对 Kotlin 中协程和延续的一些基本理解,但我找不到任何关于它们之间区别的信息
Continuation<T>.resumeWith(Result.failure(/* some exception */))
// and
Continuation<T>.resumeWithException(/* some exception */)
也一样
Continuation<T>.resumeWith(Result.success(/* some value */))
// and
Continuation<T>.resume(/* some value */)
什么时候应该使用哪一个?为什么?
这一切都归结为 Result
的用法 - 它的主要目的是封装 success/failure,因为处理来自使用 try/catch 的(大量)异步调用的异常结果变得非常混乱。
您可以通读原版 Result API Proposal on Github 以查看更多示例和创建此 api 的动机。
只需快速浏览一下源代码即可:
是的,你是对的,它完全一样。唯一的区别是您必须编写的代码量
When should which one be used?
resumeWith(Result<T>)
应该在你已经有一个 Result<T>
类型的值来封装成功或失败时使用。
否则,当您有一个 T
类型的值表示继续成功或异常表示失败时,分别使用 resume
和 resumeWithException
快捷方式会更方便。
我可能缺少对 Kotlin 中协程和延续的一些基本理解,但我找不到任何关于它们之间区别的信息
Continuation<T>.resumeWith(Result.failure(/* some exception */))
// and
Continuation<T>.resumeWithException(/* some exception */)
也一样
Continuation<T>.resumeWith(Result.success(/* some value */))
// and
Continuation<T>.resume(/* some value */)
什么时候应该使用哪一个?为什么?
这一切都归结为 Result
的用法 - 它的主要目的是封装 success/failure,因为处理来自使用 try/catch 的(大量)异步调用的异常结果变得非常混乱。
您可以通读原版 Result API Proposal on Github 以查看更多示例和创建此 api 的动机。
只需快速浏览一下源代码即可:
是的,你是对的,它完全一样。唯一的区别是您必须编写的代码量
When should which one be used?
resumeWith(Result<T>)
应该在你已经有一个 Result<T>
类型的值来封装成功或失败时使用。
否则,当您有一个 T
类型的值表示继续成功或异常表示失败时,分别使用 resume
和 resumeWithException
快捷方式会更方便。