Skip to content

PleaseClarify Event Loop Priority in Future Execution Example #6329

Open
@hashtagdisslike

Description

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.

Metadata

Assignees

No one assigned

    Labels

    from.page-issueReported in a reader-filed concern

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions