Created
April 24, 2021 21:17
-
-
Save ferazog/df581bcd88afd80a10508f46ba8e0851 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun ejecutar() { | |
// Obteniendo el usuario y la ciudad de forma asíncrona | |
val promesaDeUsuario = obtenerUsuarioDeRed() | |
val promesaDeCiudad = obtenerCiudadDeRed() | |
GlobalScope.launch { // creando un coroutine para poder ejecutar await() | |
println("hola ${promesaDeUsuario.await()} desde ${promesaDeCiudad.await()}") | |
} | |
Thread.sleep(1_500) // Espera pero menos de dos segundos, tiempo suficiente para que ambos llamados a red se completen | |
} | |
fun obtenerUsuarioDeRed(): Deferred<String> { // retorna la promesa de un String como resultado cuando la tarea termine | |
return GlobalScope.async { // Crea un scope suspendido | |
delay(1_000) | |
"Felipe" // En este ejemplo el usuario es representado por un String | |
} | |
} | |
fun obtenerCiudadDeRed(): Deferred<String> { | |
return GlobalScope.async { | |
delay(1_000) | |
"Medellin" // En este ejemplo la ciudad es representada por un String | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment