現在Async/Awaitキーワードの使える言語まとめ。
2016年12月現在だと恐らく以下の 4言語 6言語のみ、かな。
2016/12/21追記: Dart、Hack追加。
C# 5.0(.Net4.5)
VisualBasic 11.0(.Net4.5)
JavaScript ES2017(& TypeScript)
Python 3.5
Dart 1.9
Hack
JSはまだ正式ではないけど、chakra、V8が既に対応していて、BabelやTypeScriptでのトランスパイルでも使用可能。Pythonのは若干実装が異なるように見えるけどできることは同じみたい。
Hackのサンプルも用意したかったけど、環境とかドキュメント的に断念。
以下サンプル(ここから引用改変)
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
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
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
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
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();
}
実行環境
Microsoft (R) Visual C# Compiler バージョン 1.3.1.60616
Microsoft (R) Visual Basic Compiler バージョン 1.3.1.60616
nodejs/node-chakracore v7.0.0-nightly201612169538b4aa4f
Python 3.5.1