Comparing asynchronous idioms in Java, Js, Kotlin and C#
Java
Stretching Async/Await With Lambdas
Comparing the use of foreach loop without lambdas and the use of a pipeline
(such as ....map(..->...).reduce(..->...)) with lambdas and the resulting
sequential versus concurrent behavior in different programming languages
Java, Js, C# and Kotlin.
asyncfunctionfetchAndSum(...urls){letsum=0for(consturlofurls){console.log(`FETCHING from ${url}`)constres=awaitfetch(url)constbody=awaitres.text()console.log(`=======> from ${url}`)sum+=body.length}returnsum}
asyncfunctionfetchAndSumλ(...urls){returnurls.map(async(url,i)=>{console.log(`FETCHING from ${url}`)constresp=awaitfetch(url)constbody=awaitresp.text()constlength=body.lengthconsole.log(`=======> from ${urls[i]}`)returnlength}).reduce(async(prev,curr)=>awaitprev+awaitcurr)}
FETCHING from https://stackoverflow.com/
=======> from https://stackoverflow.com/
FETCHING from https://github.com/
=======> from https://github.com/
FETCHING from http://dzone.com/
=======> from http://dzone.com/
Total chars = 470831
FETCHING from https://stackoverflow.com/
FETCHING from https://github.com/
FETCHING from http://dzone.com/
=======> from https://github.com/
=======> from https://stackoverflow.com/
=======> from http://dzone.com/
Total chars = 470831
C#
staticasyncTask<int>FetchAndSum(string[]urls){intsum=0;using(HttpClienthttpClient=new HttpClient()){foreach(var url in urls){
Console.WriteLine($"FETCHING from {url}");varbody=await httpClient.GetStringAsync(url);
Console.WriteLine($"=======> from {url}");sum+= body.Length;}}returnsum;}
staticasyncTask<int>FetchAndSumλ(string[]urls){using(HttpClienthttpClient=new HttpClient()){returnawait urls
.Select(async url =>{ Console.WriteLine($"FETCHING from {url}");varbody=await httpClient.GetStringAsync(url); Console.WriteLine($"=======> from {url}");return body.Length;}).Aggregate(async(prev,curr)=>await prev +await curr);}}
FETCHING from https://stackoverflow.com/
=======> from https://stackoverflow.com/
FETCHING from https://github.com/
=======> from https://github.com/
FETCHING from http://dzone.com/
=======> from http://dzone.com/
Total chars = 470831
FETCHING from https://stackoverflow.com/
FETCHING from https://github.com/
FETCHING from http://dzone.com/
=======> from https://github.com/
=======> from https://stackoverflow.com/
=======> from http://dzone.com/
Total chars = 470831
Kotlin
suspendfunfetchAndSum(varargurls:String): Int {
var sum =0for (url in urls) {
println("FETCHING from $url")
val resp = httpClient
.sendAsync(request(url), BodyHandlers.ofString())
.await()
println("=======> from $url")
sum = sum + resp.body().length
}
return sum
}
FETCHING from https://stackoverflow.com/
=======> from https://stackoverflow.com/
FETCHING from https://github.com/
=======> from https://github.com/
FETCHING from http://dzone.com/
=======> from http://dzone.com/
Total chars = 470831
FETCHING from https://stackoverflow.com/
FETCHING from https://github.com/
FETCHING from http://dzone.com/
=======> from https://github.com/
=======> from https://stackoverflow.com/
=======> from http://dzone.com/
Total chars = 470831