async question

75 views
Skip to first unread message

Jacob Goodson

unread,
Nov 13, 2014, 6:08:30 PM11/13/14
to mi...@dartlang.org
I wrote some simple code to test the new async functionality but it is not working as I expect it to.  I thought this would print both statements roughly at the same time but it only prints mom cooks.    



Stopwatch mom = new Stopwatch();
Stopwatch dad = new Stopwatch();
Duration dur = new Duration(seconds: 1);

mymom() async {
  mom.start();
  while(true){
    if(mom.elapsed > dur){
      print('mom cooks'); //obviously
      mom.reset();
    }
  }
}

mydad() async {
  dad.start();
  while(true){
    if(dad.elapsed > dur){
      print('dad works');
      dad.reset();
    }
  } 
}

main() {
  mymom();
  mydad();
  print('also true');
}

TM

unread,
Nov 13, 2014, 7:40:38 PM11/13/14
to mi...@dartlang.org
Remember Dart is single-threaded. `Mom` executes strictly before `dad`;
there aren't two infinite loops running in parallel.

If you want to interleave the output, this will do it:

https://gist.github.com/mockturtl/97c0e692034888524f74


Notice removing the `async` modifier blows up the stack.

TM
> --
> For other discussions, see https://groups.google.com/a/dartlang.org/
>
> For HOWTO questions, visit http://stackoverflow.com/tags/dart
>
> To file a bug report or feature request, go to http://www.dartbug.com/new
>
> To unsubscribe from this group and stop receiving emails from it, send
> an email to misc+uns...@dartlang.org
> <mailto:misc+uns...@dartlang.org>.

Jacob Goodson

unread,
Nov 13, 2014, 7:44:06 PM11/13/14
to mi...@dartlang.org
Thanks for the information.  Will this eventually result in a stack overflow?
Reply all
Reply to author
Forward
0 new messages