Skip to content

Instantly share code, notes, and snippets.

@keefo
Created July 7, 2019 17:13
Show Gist options
  • Save keefo/a55f01dcf1dc6cb368364b7edbd58e6f to your computer and use it in GitHub Desktop.
Save keefo/a55f01dcf1dc6cb368364b7edbd58e6f to your computer and use it in GitHub Desktop.
async/await comparison between Kotlin, C#, Typescript
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