Dart 1.8 中的异步/等待功能 [英] Async/Await feature in Dart 1.8

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

问题描述

它在 1.8 版本中是作为 enum 之类的实验性功能还是不是?我如何在 Dart 编辑器中使用它?有没有一篇不错的文章或示例应用程序可以让我开始使用这个?

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?

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

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?

推荐答案

更新 2

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

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;
  }
}

更新

yieldyield* 在标记为 sync*(返回 Iterable)的方法中已经在 1.9.0- 中被支持edge.43534

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* 生成器函数(返回流).

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

原创

基本支持已经可用.
请参阅 https://www.dartlang.org/articles/await-async/更多详情.

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 中的异步/等待功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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