I remember I read a post in this group that fragments from async functions are scheduled using 'dart:async' scheduleMicrotask function.
But having searched through the language spec and API doc of 'dart:async', never found which API or an equivalent of which is used to schedule fragments of an async function.
I think such a detail should NOT remain as an implementation detail, and be clearly specified in the language spec. If I am wrong, please correct me.
I plan to override scheduleMicrotask to affect async function execution for some reasons. I hope to know it is legitmate rather than being an ugly hack relying on an implementation detail.
It seems that I need more clarification on my question below.
To my understanding, async functions require creation of Future, however it create one (but no fragment scheduling, I got it). And I got that you (@lrn) said it is ok to override scheduleMicrotask in a Zone to affect scheduling of async functions.
I plan to proceed with what @lrn suggests. But I'm still not sure if I'm safe to do so without any authorative statements somewhere in a spec.
2015년 6월 25일 목요일 오후 11시 49분 18초 UTC+9, DoHyung Kim 님의 말:Thanks for enlightening me. I totally misunderstood how async/await works.
But considering it is not specified how async functions are scheduled, am I still safe to override Zone's scheduleMicrotask to affect how async functions are scheduled? As you said, assume I properly run those async functions in a Zone I configure.
2015년 6월 25일 목요일 오후 11시 33분 15초 UTC+9, Lasse Reichstein Holst Nielsen 님의 말:On Thu, Jun 25, 2015 at 4:15 PM, DoHyung Kim <dyn...@gmail.com> wrote:I remember I read a post in this group that fragments from async functions are scheduled using 'dart:async' scheduleMicrotask function.The fragments, as separated by "await" gaps, are not scheduled in any particular way - they wait for the future they are waiting for to complete, and let that schedule the call that maes them continue when they have a result to continue with.But having searched through the language spec and API doc of 'dart:async', never found which API or an equivalent of which is used to schedule fragments of an async function.Basically, it's "Future.then" that does the scheduling.I think such a detail should NOT remain as an implementation detail, and be clearly specified in the language spec. If I am wrong, please correct me.I think you are wrong. :)Specifying how the async function transformation is implemented in too much detail will prevent other possible implementations.For example, it's possible to implement an async function with await using a separate stack, and simply block at the await until the future is complete. If you start requiring specific functions to be used, then it may prevent that implementation.Overspecifying is sometimes worse than underspecifying in the long run - it forces you to repeat a bad design even if you now know how to do it better (q.v. JavaScript RegExps).I plan to override scheduleMicrotask to affect async function execution for some reasons. I hope to know it is legitmate rather than being an ugly hack relying on an implementation detail.You should be fine as long as all the code you use is well-behaved. The dart:async futures do use scheduleMicrotask in the future's zone when they need to postpone a callback. If you run all your code in your own zone, the scheduleMicrotask should get called when necessary.If you have futures implementations that are not using scheduleMicrotask, or that are avoiding your zone, then it may not work, but that's not an async/await syntax related problem.It is always possible to exit your zone by doing Zone.ROOT.run(() { some code; });. There is nothing you can do about that. That's usually not the case for well-behaved code./L--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
--
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.
What I want to do is to control how an async function is scheduled.
https://dartpad.dartlang.org/199592c01dbd8e7d6427
Please see the above snippet. The 'asyncFunction()` has no await inside, but still an async function returning a Future and I have no control over how the body of the asyncFunction() is scheduled. What I want to make sure is to force async functions run after some other tasks already queued for execution.
In the DartPad above, the asyncFunction() is run after print("Out") is executed (actually after main() exited). That means the current async/await implementation does some hidden scheduling for the execution of the function body, and it is certain that the body can't run in parallel with some other codes in the same Isolate. That's engraved in somewhere in the spec.
Actually having await in an async function doesn't change the fact that anyhow the current implementation of async/await do schedule at least the initial fragment of an async function in somehow implementation specific means. See below:
https://dartpad.dartlang.org/3f9e73addf2d81b13add
print("Running") is run after the main exists.
According to what Lasse told me and what I read from the language spec, the current behavior is only single legitmate way of implementing the current spec, but other implementations may do it differently and make the order of executions in the above samples different.
If this is what the spec intends, it is ok. And I can go away from async function for my specific use case and use Future explicitly.