Skip to content

Instantly share code, notes, and snippets.

@MovileGente
Created March 21, 2019 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MovileGente/2faa5c3b3945409d2a83d09b1fc08af9 to your computer and use it in GitHub Desktop.
Save MovileGente/2faa5c3b3945409d2a83d09b1fc08af9 to your computer and use it in GitHub Desktop.
suspend fun queryUser(id: Int): User {
   delay(500) // simulando latência de rede ou outra operação lenta
   return User(id = id, name = "gente.firme", email = "talentos@movile.com", phone = 5511000000000L)
}
suspend fun sendEmail(destination: String, body: String): Boolean {
   println("Sending email to $destination with body: $body")
   delay(1000) // simulando um request/operação lenta
   val couldSentEmail = Random().nextBoolean()
   if (couldSentEmail) {
       println("Successfully sent email!")
   } else {
       println("Uh, something went terribly wrong")
   }
   return couldSentEmail
}
suspend fun sendSMS(destination: Long, message: String): Boolean {
   println("Sending sms to $destination with message: $message")
   delay(500) // simulando um request/operação lenta
   println("Successfully sent SMS!")
   return true
}
suspend fun sendMessageToUser(userId: Int, message: String) {
   val user = queryUser(userId)
   val emailStatus = sendEmail(user.email, message)
   if (!emailStatus) {
       sendSMS(user.phone, message)
   }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment