[JAVA] Async / Await syntax Summary of available languages

A summary of the languages that currently use the Async / Await keywords.

As of December 2016, probably only the following 4 languages </ s> 6 languages. 2016/12/21 postscript: Dart, Hack added.

C# 5.0(.Net4.5) VisualBasic 11.0(.Net4.5) JavaScript ES2017(& TypeScript) Python 3.5 Dart 1.9 Hack

JS is not official yet, but chakra and V8 are already supported, and it can also be used with transpile with Babel and TypeScript. Python's implementation seems to be slightly different, but the things you can do are the same. I wanted to prepare a sample of Hack, but I gave up on the environment and documents.

Sample below (Cited from here)

C#

using System;
using System.Threading;
using System.Threading.Tasks;

class Program{
	static async Task BasicAsync(){
		var catchAsync="\nSTART";
		var myAsync=(Func<string,int,Task>)(async(name,wait)=>{
			Console.WriteLine("{0}:START",name);
			await Task.Delay(wait);
			Console.WriteLine("{0}:{1}",name,wait);
			catchAsync+="=>"+name;
		});

		Console.WriteLine("Begin Async");

		var WaitA=myAsync("A",800);
		var WaitB=myAsync("B",100);
		var WaitC=myAsync("C",1500);

		await Task.WhenAll(WaitA,WaitB,WaitC);
		Console.WriteLine("--- Wall1 ---");

		var WaitD=myAsync("D",800);
		var WaitE=myAsync("E",100);
		var WaitF=myAsync("F",1500);
		var WaitGroup=new[]{WaitD,WaitE,WaitF};

		await Task.WhenAny(WaitGroup);
		Console.WriteLine("--- Wall2 ---");

		await myAsync("G",690);
		Console.WriteLine("--- Wall3 ---");

		await Task.WhenAll(WaitGroup);
		Console.WriteLine("--- Wall4 ---");

		Console.WriteLine("End Async");
		Console.WriteLine(catchAsync);
	}

	static void Main(){
		BasicAsync().Wait();
	}
}

VB

python


Imports System
Imports System.Console
Imports System.Threading
Imports System.Threading.Tasks

Module Program
	Async Function BasicAsync() As Task
		Dim catchAsync=vbLf & "START"
		Dim myAsync=Async Function(name,wait)
			WriteLine("{0}:START",name)
			Await Task.Delay(wait)
			WriteLine("{0}:{1}",name,wait)
			catchAsync &= "=>" & name
		End Function

		WriteLine("Begin Async")

		Dim WaitA=myAsync("A",800)
		Dim WaitB=myAsync("B",100)
		Dim WaitC=myAsync("C",1500)

		Await Task.WhenAll(WaitA,WaitB,WaitC)
		WriteLine("--- Wall1 ---")

		Dim WaitD=myAsync("D",800)
		Dim WaitE=myAsync("E",100)
		Dim WaitF=myAsync("F",1500)
		Dim WaitGroup={WaitD,WaitE,WaitF}

		Await Task.WhenAny(WaitGroup)
		WriteLine("--- Wall2 ---")

		Await myAsync("G",690)
		WriteLine("--- Wall3 ---")

		Await Task.WhenAll(WaitGroup)
		WriteLine("--- Wall4 ---")

		WriteLine("End Async")
		WriteLine(catchAsync)
	End Function

	Sub Main()
		BasicAsync().Wait()
	End Sub
End Module

JavaScript

python


async function BasicAsync(){
	var catchAsync="\nSTART";
	const myAsync=async (name,wait)=>{
		console.log("%s:START",name);
		await new Promise(resolve=>setTimeout(resolve,wait));
		console.log("%s:%d",name,wait);
		catchAsync+="=>"+name;
	};

	console.log("Begin Async");

	const WaitA=myAsync("A",800);
	const WaitB=myAsync("B",100);
	const WaitC=myAsync("C",1500);

	await Promise.all([WaitA,WaitB,WaitC]);
	console.log("--- Wall1 ---");

	const WaitD=myAsync("D",800);
	const WaitE=myAsync("E",100);
	const WaitF=myAsync("F",1500);
	const WaitGroup=[WaitD,WaitE,WaitF];

	await Promise.race(WaitGroup);
	console.log("--- Wall2 ---");

	await myAsync("G",690);
	console.log("--- Wall3 ---");

	await Promise.all(WaitGroup);
	console.log("--- Wall4 ---");

	console.log("End Async");
	console.log(catchAsync);
}

(function(){
	BasicAsync();
})();

Python

python


import asyncio

catchAsync="\nSTART"
async def BasicAsync():
	async def myAsync(name,wait):
		global catchAsync
		print("{0}:START".format(name))
		await asyncio.sleep(wait/1000)
		print("{0}:{1}".format(name,wait))
		catchAsync+="=>"+name

	print("Begin Async")

	WaitA=asyncio.ensure_future(myAsync("A",800))
	WaitB=asyncio.ensure_future(myAsync("B",100))
	WaitC=asyncio.ensure_future(myAsync("C",1500))

	await asyncio.wait([WaitA,WaitB,WaitC])
	print("--- Wall1 ---")

	WaitD=asyncio.ensure_future(myAsync("D",800))
	WaitE=asyncio.ensure_future(myAsync("E",100))
	WaitF=asyncio.ensure_future(myAsync("F",1500))
	WaitGroup=[WaitD,WaitE,WaitF]

	await asyncio.wait(WaitGroup,return_when=asyncio.FIRST_COMPLETED)
	print("--- Wall2 ---")

	await asyncio.ensure_future(myAsync("G",690))
	print("--- Wall3 ---")

	await asyncio.wait(WaitGroup)
	print("--- Wall4 ---")

	print("End Async")
	print(catchAsync)

if __name__=="__main__":
	loop=asyncio.get_event_loop()
	loop.run_until_complete(BasicAsync())

Dart

python


import "dart:async";

Future BasicAsync() async{
	var catchAsync="\nSTART";
	var myAsync=(name,wait) async{
		print("$name:START");
		await new Future.delayed(new Duration(milliseconds:wait));
		print("$name:$wait");
		catchAsync+="=>"+name;
	};

	print("Begin Async");

	var WaitA=myAsync("A",800);
	var WaitB=myAsync("B",100);
	var WaitC=myAsync("C",1500);

	await Future.wait([WaitA,WaitB,WaitC]);
	print("--- Wall1 ---");

	var WaitD=myAsync("D",800);
	var WaitE=myAsync("E",100);
	var WaitF=myAsync("F",1500);
	var WaitGroup=[WaitD,WaitE,WaitF];

	await Future.any(WaitGroup);
	print("--- Wall2 ---");

	await myAsync("G",690);
	print("--- Wall3 ---");

	await Future.wait(WaitGroup);
	print("--- Wall4 ---");

	print("End Async");
	print(catchAsync);
}

main() {
	BasicAsync();
}

Execution environment

Microsoft (R) Visual C # Compiler version 1.3.1.60616 Microsoft (R) Visual Basic Compiler version 1.3.1.60616 nodejs/node-chakracore v7.0.0-nightly201612169538b4aa4f Python 3.5.1

Recommended Posts

Async / Await syntax Summary of available languages
[Summary of 27 languages] My number check digit calculation method
Python asynchronous processing ~ Full understanding of async and await ~
Numerical summary of data
Summary of Tensorflow / Keras
Summary of pyenv usage
python async / await curio
Summary of Python arguments
Summary of logrotate software logrotate
Summary of test method
[For beginners] A word summary of popular programming languages (2018 version)