Last active
April 20, 2021 09:10
-
-
Save davidmigloz/86400f83522fe67d8e8fe53d57fac8b6 to your computer and use it in GitHub Desktop.
Dart vs Kotlin: async concurrent
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
Future<String> loadImage(String name) async { | |
await Future.delayed(Duration(seconds: 1)); | |
return "IMAGE$name"; | |
} | |
displayImages(String image1, String image2) { | |
print("$image1 | $image2"); | |
} | |
void main() async { | |
Future<String> image1Future = loadImage("1.png"); | |
Future<String> image2Future = loadImage("2.png"); | |
String image1 = await image1Future; | |
String image2 = await image2Future; | |
displayImages(image1, image2); | |
} |
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
import kotlinx.coroutines.* | |
suspend fun loadImage(name: String): String { | |
delay(1000) | |
return "IMAGE$name" | |
} | |
fun displayImages(image1: String, image2: String) { | |
println("$image1 | $image2"); | |
} | |
fun main() = runBlocking { | |
var image1Deferred = async { loadImage("1.png") } | |
var image2Deferred = async { loadImage("2.png") } | |
var image1: String = image1Deferred.await() | |
var image2: String = image2Deferred.await() | |
displayImages(image1, image2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment