Created
July 7, 2019 17:13
-
-
Save keefo/a55f01dcf1dc6cb368364b7edbd58e6f to your computer and use it in GitHub Desktop.
async/await comparison between Kotlin, C#, Typescript
This file contains hidden or 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.delay | |
fun main() { | |
fireAndForget() | |
} | |
/* C# | |
public static async Task Main(string[] args) | |
{ | |
fireAndForget(); | |
} | |
*/ | |
/* Typescript | |
function main(...args: string[]) { | |
fireAndForget().then() | |
} | |
*/ | |
fun fireAndForget() = launch { // GlobalScope.launch(Dispatchers.Main) | |
val data = fetchDataAsync().await() // GlobalScope.async | |
println(data) | |
} | |
/* C# | |
async Task fireAndForget() | |
{ | |
var data = await fetchDataAsync(); | |
Console.WriteLine(data); | |
} | |
*/ | |
/* Typescript | |
async function fireAndForget() { | |
const data = await fetchDataAsync() | |
console.log(data) | |
} | |
*/ | |
fun fetchDataAsync() = async { | |
delay(3_000) | |
return@async "Hello Coroutine!" | |
} | |
/* C# | |
async Task<string> fetchDataAsync() | |
{ | |
await Task.Delay(1000); | |
return "Hello Coroutine!"; | |
} | |
*/ | |
/* Typescript | |
async function fetchDataAsync() { | |
await delay(1_000) | |
return "Hello Coroutine!" | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment