Un résumé des langues qui utilisent actuellement les mots clés Async / Await.
En décembre 2016, probablement seulement les 4 langues </ s> 6 langues suivantes.
2016/12/21 postscript: Dart, Hack ajouté.
C# 5.0(.Net4.5) VisualBasic 11.0(.Net4.5) JavaScript ES2017(& TypeScript) Python 3.5 Dart 1.9 Hack
JS n'est pas encore officiel, mais chakra et V8 sont déjà pris en charge et peuvent être utilisés avec transpile avec Babel et TypeScript. Python semble avoir une implémentation légèrement différente, mais il semble pouvoir faire de même. Je voulais préparer un échantillon de Hack, mais j'ai abandonné l'environnement et les documents.
Exemple ci-dessous (Modifié à partir d'ici)
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();
}
Compilateur Microsoft (R) Visual C # version 1.3.1.60616 Compilateur Microsoft (R) Visual Basic version 1.3.1.60616 nodejs/node-chakracore v7.0.0-nightly201612169538b4aa4f Python 3.5.1
Recommended Posts