Use Future-Based APIs

73 views
Skip to first unread message

Massimiliano Tomassoli

unread,
Apr 19, 2014, 7:24:22 AM4/19/14
to mi...@dartlang.org
Hi,
I'm reading the article Use Future-Based APIs.
Let's consider the following example:

  import 'dart:io';
  import 'dart:async';

  void printDailyNewsDigest() {
    File file = new File("dailyNewsDigest.txt");
    Future future = file.readAsString();
    future.then((content) {
      print(content);
    });
  }

void main() { printDailyNewsDigest(); printWinningLotteryNumbers(); printWeatherForecast(); printBaseballScore(); }

I'd like to know whether the callback in printDailyNewsDigest might be called in the middle of one of the other three printing functions.

Lasse R.H. Nielsen

unread,
Apr 19, 2014, 7:40:34 AM4/19/14
to mi...@dartlang.org
It won't. Events wont happen in the middle of running code [1].

If the other three print functions are also async, the callbacks may be called in any order, and any interleaving, if they have more than one callback, but they won't be interrupting each other.

If the other print functions are not async, they will definitely complete before the news digest callback is called.


/L
[1] Unless someone is using a sync completer in a non-recommended way. In that case, it should be reported as a bug. Sync completers are intended to be used to turn one event turn into another without going back through the event loop, but the callbacks shouldn't be able to tell the difference.


--
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.



--
Lasse R.H. Nielsen - l...@google.com  
'Faith without judgement merely degrades the spirit divine'
Google Denmark ApS - Frederiksborggade 20B, 1 sal - 1360 København K - Denmark - CVR nr. 28 86 69 84

Massimiliano Tomassoli

unread,
Apr 19, 2014, 7:55:52 AM4/19/14
to mi...@dartlang.org
I have another doubt. What happens when the main function terminates and the future is still "incomplete"? Does the main function wait for all the pending futures to complete before exiting?

Jacob Bang

unread,
Apr 19, 2014, 2:18:56 PM4/19/14
to mi...@dartlang.org
"Does the main function wait for all the pending futures to complete before exiting?"

Yes the Dart program will wait for all pending futures to complete and all non complete streams have no listeners.

Jacob Bang / julemand101

Massimiliano Tomassoli

unread,
Apr 19, 2014, 2:23:38 PM4/19/14
to mi...@dartlang.org
On Saturday, April 19, 2014 8:18:56 PM UTC+2, Jacob Bang wrote:
"Does the main function wait for all the pending futures to complete before exiting?"

Yes the Dart program will wait for all pending futures to complete and all non complete streams have no listeners.

OK, thanks.

Reply all
Reply to author
Forward
0 new messages