Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Await inside for loop is admitted in Dart?

I have a program like the following:

main() async {   ooClass = new OoClass(1);   int val = await function1();   print(val);   ooClass = new OoClass(2);   val = await function1();   print(val);   ooClass = new OoClass(3);   val = await function1();   print(val);  }  OoClass ooClass;  Future<int> function1() async {   List list3 = await function2();   return list3.indexOf('Ok'); }   Future<List<String>> function2() async {    List<String> list1 = new List<String>();    function3(Map<String, int> map1) async {     String string1 = '';     bool bool1 = false;     List<String> list2 = [];     String string2;      function4(String string3) async {       if (ooClass.function7(string3)) return;       if (ooClass.function8() && !bool1) {         bool1 = true;         return;       }       string2 = await function5(string3);       list2.add(string2);     }      for (String key in map1.keys) {       await function4(key);     }     string1 = list2.join(', ');     list1.add(string1);   }    for (Map<String, int> idxList in ooClass.function6()) {     await function3(idxList);   }   return list1; }  function5(String s1) {   return new Future.value('Ok'); }  class OoClass {    List<Map<String, int>> map2;   bool bool3 = false;    OoClass(int type) {     switch(type) {       case 1:         map2 = [{'Ok':1}];         break;       case 2:         map2 = [{'id': 1, 'Ok':1}];         break;       case 3:         map2 = [{'foo': 1, 'Ok':1}];         bool3 = true;         break;     }   }    List<Map<String, int>> function6() {     return map2;   }    bool function7(String string9) {     if (string9 == 'id') return true;     return false;   }    bool function8() {     return bool3;   } } 

This snippet works perfectly.

In my real environment, instead, when await function4(key); is called, function2 returns the list1 List (empty). Function4 call is executed later but the result of function2 is lost.

I don't really understand this behavior. Could it be a bug or await inside for loop is not to be used? If await should not be used inside for loop how could I implement it in another way?

I'm using dart 1.22.0-dev.4 but I've tried also with older (and stable) versions and I had the same result.


I finally got the problem and it did not depend on await in a for loop. It was instead an error in my code.

like image 863
J F Avatar asked Dec 29 '16 08:12

J F


People also ask

Can await be used inside for loop?

When you use await , you expect JavaScript to pause execution until the awaited promise gets resolved. This means await s in a for-loop should get executed in series. The result is what you'd expect. This behaviour works with most loops (like while and for-of loops)…

Does await in a for loop block?

No, await won't block the looping.

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 are the async and await keywords for in Dart?

Async and Await keywords are used to provide a declarative way to define the asynchronous function and use their results. The async keyword is used when we want to declare a function as asynchronous and the await keyword is used only on asynchronous functions.


1 Answers

Yes, await is permitted inside a for loop in Dart, and it will work as expected.

for (var o in objects) {   await doSomething(o); } 

And there is even await for for Streams, if that's what you're looking for:

await for (var event in eventStream) {   print("Event received: $event"); } 

Your example works correctly in DartPad. It's too complex & abstract to debug but, at least superficially, it should work. You say that the snippet doesn't work in your "real environment", though. Maybe we could help if you explained what you mean by that?

Additional tip: take full advantage of static analysis, especially the await_only_futures and unawaited_futures linter rules. This can help you catch many bugs.

like image 180
filiph Avatar answered Sep 25 '22 09:09

filiph