Dart 1.8中的Async / Await功能 [英] Async/Await feature in Dart 1.8

查看:105
本文介绍了Dart 1.8中的Async / Await功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它是在1.8版本中作为实验功能像枚举还是不是?如何在Dart编辑器中使用它?有没有一个很好的文章或示例应用程序,可以让我开始这个?



当它仍然是一个实验功能,推荐的pub包?是否可以在pub包中使用该功能?

解决方案

更新2



最近的每晚构建也支持 async *

  void main(){
generate()。listen((i)=> print(i));
}

Stream< int> generate()async * {
int i = 0;

for(int i = 0; i <100; i ++){
yield ++ i;
}
}

更新 >

yield yield * c> sync * (返回Iterable)已在 1.9.0-edge.43534

中支持

  void main(){
var x = concat([0,1,2,3,4] 6,7,8,9]);
// x是迭代值1到9的Iterable
print(x);
}

//标记为`sync *`的方法返回一个`Iterable'
concat(Iterable left,Iterable right)sync * {
yield * left;
yield * right;
}





  void main(){
print(filter([0,1,2,3,4,5],(x)=> x.isEven)
}

filter(ss,p)sync * {
for(var s in ss){
if(p(s))yield s;
}
}

async *
$ b

b

基本支持已可用。

请参见 https:// www.dartlang.org/articles/await-async/ 了解详情。

  main()async {

// await
print(await foo());
try {
print(await fooThrows());
} catch(e){
print(e);
}

// await for
var stream = new Stream.fromIterable([1,2,3,4,5]);
await for(var e in stream){
print(e);
}
}

foo()async => 42;

fooThrows()async =>抛出任何东西


Is it in the 1.8 release as an experimental feature like enum or is it not? And how can I use it in the Dart Editor? Is there a nice article or example app that can get me started with this?

When it is still an experimental feature what is recommended for pub packages? Is it fine to use that feature in pub packages or not?

解决方案

Update 2

The most recent nightly build also supports async*

void main() {
  generate().listen((i) => print(i));
}

Stream<int> generate () async* {
  int i = 0;

  for(int i = 0; i < 100; i++) {
    yield ++i;
  }
}

Update

yield and yield* in a method marked sync* (returning an Iterable) are already supported in 1.9.0-edge.43534

void main() {
  var x = concat([0, 1, 2, 3, 4], [5, 6, 7, 8, 9]);
  // x is an Iterable which iterates over the values 1 to 9
  print(x);
}

// A method marked `sync*` returns an `Iterable`
concat(Iterable left, Iterable right) sync* {
  yield* left;
  yield* right;
}

void main() {
  print(filter([0, 1, 2, 3, 4, 5], (x) => x.isEven));
}

filter(ss, p) sync* {
  for (var s in ss) {
    if (p(s)) yield s;
  }
}

async* generator functions (returning a Stream) are not yet supported.

Original

Basic support is already available.
See https://www.dartlang.org/articles/await-async/ for more details.

main() async {

  // await
  print(await foo());
  try {
    print(await fooThrows());
  } catch(e) {
    print(e);
  }

  // await for
  var stream = new Stream.fromIterable([1,2,3,4,5]); 
  await for (var e in stream) { 
    print(e); 
  }
}

foo() async => 42;

fooThrows() async => throw 'Anything';

这篇关于Dart 1.8中的Async / Await功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆