Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PleaseClarify Event Loop Priority in Future Execution Example #6329

Open
1 task done
hashtagdisslike opened this issue Jan 14, 2025 · 0 comments
Open
1 task done

PleaseClarify Event Loop Priority in Future Execution Example #6329

hashtagdisslike opened this issue Jan 14, 2025 · 0 comments
Labels
from.page-issue Reported in a reader-filed concern

Comments

@hashtagdisslike
Copy link

hashtagdisslike commented Jan 14, 2025

Page URL

https://dart.dev/libraries/async/async-await#example-introducing-futures

Page source

No response

Describe the problem

In your documentation, it says:
`Future fetchUserOrder() {
// Imagine that this function is fetching user info from another service or database.
return Future.delayed(const Duration(seconds: 2), () => print('Large Latte'));
}

void main() {
fetchUserOrder();
print('Fetching user order...');
}`

"In the preceding example, even though fetchUserOrder() executes before the print() call on line 8, the console shows the output from line 8("Fetching user order...") before the output from fetchUserOrder() ("Large Latte"). This is because fetchUserOrder() delays before it prints "Large Latte"."

However, I believe the statement "Large Latte" being printed after "Fetching User Order..." isn't happening due to the specified delay of two seconds, but rather due to it being the method of a Future Object, which causes its code to enter the event queue and subsequently be executed only after all synchronous code has been executed, which would have been the case even if the duration specified had been 0 seconds.

Expected fix

Change the sentence "This is because fetchUserOrder() delays before it prints "Large Latte"." to "This is because fetchUserOrder() is asynchronous code which always is executed after synchronous code is, even without any specified delay.".

Additional context

No response

I would like to fix this problem.

  • I will try and fix this problem on dart.dev.
@hashtagdisslike hashtagdisslike added the from.page-issue Reported in a reader-filed concern label Jan 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
from.page-issue Reported in a reader-filed concern
Projects
None yet
Development

No branches or pull requests

1 participant