Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. fun start() {
  2. GlobalScope.launch {
  3. try {
  4. val def1 = async { doWork1() }
  5. val def2 = async { doWork2() }
  6. Log.d("test", "Result ${def1.await()}, ${def2.await()}")
  7. } catch (ex: Exception) {
  8. Log.e("test", "Error", ex)
  9. }
  10. }
  11. }
  12.  
  13. private suspend fun doWork1(): Int {
  14. delay(1000L)
  15. throw Exception("work 1")
  16. }
  17.  
  18. private suspend fun doWork2(): Int {
  19. delay(1000L)
  20. return 1
  21. }
  22.  
  23. fun start() {
  24. GlobalScope.launch {
  25. try {
  26. val value = doWork1()
  27. Log.d("test", "Result: $value")
  28. } catch (ex: Exception) {
  29. Log.e("test", "Error", ex)
  30. }
  31. }
  32. }
  33.  
  34. private suspend fun doWork1(): Int {
  35. delay(1000L)
  36. throw Exception("work 1")
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement