Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Async/Await feature in Dart 1.8

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?

like image 499
Joris Hermans Avatar asked Nov 30 '14 10:11

Joris Hermans


People also ask

What does async and await do in Dart?

Summary of asynchronous programming in Dart Asynchronous function is a function that returns the type of Future. We put await in front of an asynchronous function to make the subsequence lines waiting for that future's result. We put async before the function body to mark that the function support await .

What does await do in Dart?

When you await an asynchronous function, the execution of the code within the caller suspends while the async operation is executed. When the operation is completed, the value of what was awaited is contained within a Future object.

What does async mean in Dart?

Advertisements. An asynchronous operation executes in a thread, separate from the main application thread. When an application calls a method to perform an operation asynchronously, the application can continue executing while the asynchronous method performs its task.

What's the difference between async and async * in Dart?

What is the difference between async and async* in Dart? The difference between both is that async* will always return a Stream and offer some syntax sugar to emit a value through the yield keyword. async gives you a Future and async* gives you a Stream.


1 Answers

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';
like image 92
Günter Zöchbauer Avatar answered Oct 30 '22 10:10

Günter Zöchbauer