async/await - not waiting before future() - Dart
up vote
1
down vote
favorite
Based on Dart official page When using Async/Wait:
When the app sees the word async
it execute the function normally synchronously, until it sees await
or return
Note that an async function starts executing right away
(synchronously). The function suspends execution and returns an
uncompleted future when it reaches the first occurrence of any of the
following:
- The function’s first await expression (after the function gets the
uncompleted future from that expression).
- Any return statement in the function.
- The end of the function body.
and when it sees any of them it returns an uncompleted Future
and stops executing the async
function until it execute all of the other functions, and when all of the other functions are executed, the app goes back to the async
function and executes what's inside of it.
Here's a photo from the Dart official page explaining it in more details:
But when I was testing that I tried to add a print
statement before returning the future result as you can see in the below code, but the result wasn't as stated in the site, as it's saying that the app stops executing once it sees the word await
but the statement: "Async - Hi Called 1st" was printed as you can see before the other functions were executed.
import 'dart:async';
Future<void> print1stAsync() async {
var test = await callAsync();
print(test);
}
main() {
print1stAsync();
print2nd();
print3rd();
print4th();
}
print2nd() {
print('Called 2nd');
}
print3rd() {
print("Called 3rd");
}
print4th() {
print('Called 4th');
}
Future<String> callAsync() {
print("Async - Hi Called 1st");
return Future(() => "Async - Called 1st ");
}
Output:
Async - Hi Called 1st
Called 2nd
Called 3rd
Called 4th
Async - Called 1st
So why is this happening? have I miss understood something?
asynchronous dart async-await flutter
add a comment |
up vote
1
down vote
favorite
Based on Dart official page When using Async/Wait:
When the app sees the word async
it execute the function normally synchronously, until it sees await
or return
Note that an async function starts executing right away
(synchronously). The function suspends execution and returns an
uncompleted future when it reaches the first occurrence of any of the
following:
- The function’s first await expression (after the function gets the
uncompleted future from that expression).
- Any return statement in the function.
- The end of the function body.
and when it sees any of them it returns an uncompleted Future
and stops executing the async
function until it execute all of the other functions, and when all of the other functions are executed, the app goes back to the async
function and executes what's inside of it.
Here's a photo from the Dart official page explaining it in more details:
But when I was testing that I tried to add a print
statement before returning the future result as you can see in the below code, but the result wasn't as stated in the site, as it's saying that the app stops executing once it sees the word await
but the statement: "Async - Hi Called 1st" was printed as you can see before the other functions were executed.
import 'dart:async';
Future<void> print1stAsync() async {
var test = await callAsync();
print(test);
}
main() {
print1stAsync();
print2nd();
print3rd();
print4th();
}
print2nd() {
print('Called 2nd');
}
print3rd() {
print("Called 3rd");
}
print4th() {
print('Called 4th');
}
Future<String> callAsync() {
print("Async - Hi Called 1st");
return Future(() => "Async - Called 1st ");
}
Output:
Async - Hi Called 1st
Called 2nd
Called 3rd
Called 4th
Async - Called 1st
So why is this happening? have I miss understood something?
asynchronous dart async-await flutter
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Based on Dart official page When using Async/Wait:
When the app sees the word async
it execute the function normally synchronously, until it sees await
or return
Note that an async function starts executing right away
(synchronously). The function suspends execution and returns an
uncompleted future when it reaches the first occurrence of any of the
following:
- The function’s first await expression (after the function gets the
uncompleted future from that expression).
- Any return statement in the function.
- The end of the function body.
and when it sees any of them it returns an uncompleted Future
and stops executing the async
function until it execute all of the other functions, and when all of the other functions are executed, the app goes back to the async
function and executes what's inside of it.
Here's a photo from the Dart official page explaining it in more details:
But when I was testing that I tried to add a print
statement before returning the future result as you can see in the below code, but the result wasn't as stated in the site, as it's saying that the app stops executing once it sees the word await
but the statement: "Async - Hi Called 1st" was printed as you can see before the other functions were executed.
import 'dart:async';
Future<void> print1stAsync() async {
var test = await callAsync();
print(test);
}
main() {
print1stAsync();
print2nd();
print3rd();
print4th();
}
print2nd() {
print('Called 2nd');
}
print3rd() {
print("Called 3rd");
}
print4th() {
print('Called 4th');
}
Future<String> callAsync() {
print("Async - Hi Called 1st");
return Future(() => "Async - Called 1st ");
}
Output:
Async - Hi Called 1st
Called 2nd
Called 3rd
Called 4th
Async - Called 1st
So why is this happening? have I miss understood something?
asynchronous dart async-await flutter
Based on Dart official page When using Async/Wait:
When the app sees the word async
it execute the function normally synchronously, until it sees await
or return
Note that an async function starts executing right away
(synchronously). The function suspends execution and returns an
uncompleted future when it reaches the first occurrence of any of the
following:
- The function’s first await expression (after the function gets the
uncompleted future from that expression).
- Any return statement in the function.
- The end of the function body.
and when it sees any of them it returns an uncompleted Future
and stops executing the async
function until it execute all of the other functions, and when all of the other functions are executed, the app goes back to the async
function and executes what's inside of it.
Here's a photo from the Dart official page explaining it in more details:
But when I was testing that I tried to add a print
statement before returning the future result as you can see in the below code, but the result wasn't as stated in the site, as it's saying that the app stops executing once it sees the word await
but the statement: "Async - Hi Called 1st" was printed as you can see before the other functions were executed.
import 'dart:async';
Future<void> print1stAsync() async {
var test = await callAsync();
print(test);
}
main() {
print1stAsync();
print2nd();
print3rd();
print4th();
}
print2nd() {
print('Called 2nd');
}
print3rd() {
print("Called 3rd");
}
print4th() {
print('Called 4th');
}
Future<String> callAsync() {
print("Async - Hi Called 1st");
return Future(() => "Async - Called 1st ");
}
Output:
Async - Hi Called 1st
Called 2nd
Called 3rd
Called 4th
Async - Called 1st
So why is this happening? have I miss understood something?
asynchronous dart async-await flutter
asynchronous dart async-await flutter
edited Nov 11 at 14:53
asked Nov 11 at 14:48
Yousef Gamal
6410
6410
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
The app doesn't stop executing, only the execution of the code after await
is delayed until the returned Future
completes.
You also need to await the call to the async function print1stAsync()
, otherwise the execution of main
continues after the call to callAsync();
(after the call, not after the Future
it returns completes)
main() async {
await print1stAsync();
print2nd();
print3rd();
print4th();
}
Add asnyc
to a function also means that this function returns a Future
.
There is no way to go back from async to sync. Async is contagious.
await callAsync();
means code below that line within the same function (like print(test);
in your example) will be delayed.
It doesn't say anything about code in callAsync() or code that calls print1stAsync();
1
Is it okay to usevoid
as return type instead ofFuture<Null>
when not returning any value cause my IDE doesn't show any error and the code works same in both cases.
– CopsOnRoad
Nov 11 at 15:02
1
Yes it is ok. This signals fire-and-forget - the function communicates that it does not expected to be awaited. It's rather seldom used, but it's valid.
– Günter Zöchbauer
Nov 11 at 15:04
2
I'm not sure I understand your question.print("Async - Hi Called 1st");
is executed immediately. There is noawait
before execution reaches that line. TheFuture
returned fromprint1stAsync
is completed when everyawait
edFuture
inprint1stAsync
is completed. If it is not awaited, code inmain
continues to execute async results inprintAsync()
are being waited for.
– Günter Zöchbauer
Nov 11 at 15:15
2
await callAsync();
means code below that line within the same function (likeprint(test);
in your example) will be delayed. It doesn't say anything about code incallAsync()
or code that callsprint1stAsync
.
– Günter Zöchbauer
Nov 11 at 15:46
1
I understand it perfectly now, That was the magic sentence " await doesn't say anything about code in callAsync()". Thank you very much.
– Yousef Gamal
Nov 11 at 15:59
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
The app doesn't stop executing, only the execution of the code after await
is delayed until the returned Future
completes.
You also need to await the call to the async function print1stAsync()
, otherwise the execution of main
continues after the call to callAsync();
(after the call, not after the Future
it returns completes)
main() async {
await print1stAsync();
print2nd();
print3rd();
print4th();
}
Add asnyc
to a function also means that this function returns a Future
.
There is no way to go back from async to sync. Async is contagious.
await callAsync();
means code below that line within the same function (like print(test);
in your example) will be delayed.
It doesn't say anything about code in callAsync() or code that calls print1stAsync();
1
Is it okay to usevoid
as return type instead ofFuture<Null>
when not returning any value cause my IDE doesn't show any error and the code works same in both cases.
– CopsOnRoad
Nov 11 at 15:02
1
Yes it is ok. This signals fire-and-forget - the function communicates that it does not expected to be awaited. It's rather seldom used, but it's valid.
– Günter Zöchbauer
Nov 11 at 15:04
2
I'm not sure I understand your question.print("Async - Hi Called 1st");
is executed immediately. There is noawait
before execution reaches that line. TheFuture
returned fromprint1stAsync
is completed when everyawait
edFuture
inprint1stAsync
is completed. If it is not awaited, code inmain
continues to execute async results inprintAsync()
are being waited for.
– Günter Zöchbauer
Nov 11 at 15:15
2
await callAsync();
means code below that line within the same function (likeprint(test);
in your example) will be delayed. It doesn't say anything about code incallAsync()
or code that callsprint1stAsync
.
– Günter Zöchbauer
Nov 11 at 15:46
1
I understand it perfectly now, That was the magic sentence " await doesn't say anything about code in callAsync()". Thank you very much.
– Yousef Gamal
Nov 11 at 15:59
|
show 2 more comments
up vote
2
down vote
accepted
The app doesn't stop executing, only the execution of the code after await
is delayed until the returned Future
completes.
You also need to await the call to the async function print1stAsync()
, otherwise the execution of main
continues after the call to callAsync();
(after the call, not after the Future
it returns completes)
main() async {
await print1stAsync();
print2nd();
print3rd();
print4th();
}
Add asnyc
to a function also means that this function returns a Future
.
There is no way to go back from async to sync. Async is contagious.
await callAsync();
means code below that line within the same function (like print(test);
in your example) will be delayed.
It doesn't say anything about code in callAsync() or code that calls print1stAsync();
1
Is it okay to usevoid
as return type instead ofFuture<Null>
when not returning any value cause my IDE doesn't show any error and the code works same in both cases.
– CopsOnRoad
Nov 11 at 15:02
1
Yes it is ok. This signals fire-and-forget - the function communicates that it does not expected to be awaited. It's rather seldom used, but it's valid.
– Günter Zöchbauer
Nov 11 at 15:04
2
I'm not sure I understand your question.print("Async - Hi Called 1st");
is executed immediately. There is noawait
before execution reaches that line. TheFuture
returned fromprint1stAsync
is completed when everyawait
edFuture
inprint1stAsync
is completed. If it is not awaited, code inmain
continues to execute async results inprintAsync()
are being waited for.
– Günter Zöchbauer
Nov 11 at 15:15
2
await callAsync();
means code below that line within the same function (likeprint(test);
in your example) will be delayed. It doesn't say anything about code incallAsync()
or code that callsprint1stAsync
.
– Günter Zöchbauer
Nov 11 at 15:46
1
I understand it perfectly now, That was the magic sentence " await doesn't say anything about code in callAsync()". Thank you very much.
– Yousef Gamal
Nov 11 at 15:59
|
show 2 more comments
up vote
2
down vote
accepted
up vote
2
down vote
accepted
The app doesn't stop executing, only the execution of the code after await
is delayed until the returned Future
completes.
You also need to await the call to the async function print1stAsync()
, otherwise the execution of main
continues after the call to callAsync();
(after the call, not after the Future
it returns completes)
main() async {
await print1stAsync();
print2nd();
print3rd();
print4th();
}
Add asnyc
to a function also means that this function returns a Future
.
There is no way to go back from async to sync. Async is contagious.
await callAsync();
means code below that line within the same function (like print(test);
in your example) will be delayed.
It doesn't say anything about code in callAsync() or code that calls print1stAsync();
The app doesn't stop executing, only the execution of the code after await
is delayed until the returned Future
completes.
You also need to await the call to the async function print1stAsync()
, otherwise the execution of main
continues after the call to callAsync();
(after the call, not after the Future
it returns completes)
main() async {
await print1stAsync();
print2nd();
print3rd();
print4th();
}
Add asnyc
to a function also means that this function returns a Future
.
There is no way to go back from async to sync. Async is contagious.
await callAsync();
means code below that line within the same function (like print(test);
in your example) will be delayed.
It doesn't say anything about code in callAsync() or code that calls print1stAsync();
edited Nov 11 at 16:15
Yousef Gamal
6410
6410
answered Nov 11 at 14:56
Günter Zöchbauer
309k64914861
309k64914861
1
Is it okay to usevoid
as return type instead ofFuture<Null>
when not returning any value cause my IDE doesn't show any error and the code works same in both cases.
– CopsOnRoad
Nov 11 at 15:02
1
Yes it is ok. This signals fire-and-forget - the function communicates that it does not expected to be awaited. It's rather seldom used, but it's valid.
– Günter Zöchbauer
Nov 11 at 15:04
2
I'm not sure I understand your question.print("Async - Hi Called 1st");
is executed immediately. There is noawait
before execution reaches that line. TheFuture
returned fromprint1stAsync
is completed when everyawait
edFuture
inprint1stAsync
is completed. If it is not awaited, code inmain
continues to execute async results inprintAsync()
are being waited for.
– Günter Zöchbauer
Nov 11 at 15:15
2
await callAsync();
means code below that line within the same function (likeprint(test);
in your example) will be delayed. It doesn't say anything about code incallAsync()
or code that callsprint1stAsync
.
– Günter Zöchbauer
Nov 11 at 15:46
1
I understand it perfectly now, That was the magic sentence " await doesn't say anything about code in callAsync()". Thank you very much.
– Yousef Gamal
Nov 11 at 15:59
|
show 2 more comments
1
Is it okay to usevoid
as return type instead ofFuture<Null>
when not returning any value cause my IDE doesn't show any error and the code works same in both cases.
– CopsOnRoad
Nov 11 at 15:02
1
Yes it is ok. This signals fire-and-forget - the function communicates that it does not expected to be awaited. It's rather seldom used, but it's valid.
– Günter Zöchbauer
Nov 11 at 15:04
2
I'm not sure I understand your question.print("Async - Hi Called 1st");
is executed immediately. There is noawait
before execution reaches that line. TheFuture
returned fromprint1stAsync
is completed when everyawait
edFuture
inprint1stAsync
is completed. If it is not awaited, code inmain
continues to execute async results inprintAsync()
are being waited for.
– Günter Zöchbauer
Nov 11 at 15:15
2
await callAsync();
means code below that line within the same function (likeprint(test);
in your example) will be delayed. It doesn't say anything about code incallAsync()
or code that callsprint1stAsync
.
– Günter Zöchbauer
Nov 11 at 15:46
1
I understand it perfectly now, That was the magic sentence " await doesn't say anything about code in callAsync()". Thank you very much.
– Yousef Gamal
Nov 11 at 15:59
1
1
Is it okay to use
void
as return type instead of Future<Null>
when not returning any value cause my IDE doesn't show any error and the code works same in both cases.– CopsOnRoad
Nov 11 at 15:02
Is it okay to use
void
as return type instead of Future<Null>
when not returning any value cause my IDE doesn't show any error and the code works same in both cases.– CopsOnRoad
Nov 11 at 15:02
1
1
Yes it is ok. This signals fire-and-forget - the function communicates that it does not expected to be awaited. It's rather seldom used, but it's valid.
– Günter Zöchbauer
Nov 11 at 15:04
Yes it is ok. This signals fire-and-forget - the function communicates that it does not expected to be awaited. It's rather seldom used, but it's valid.
– Günter Zöchbauer
Nov 11 at 15:04
2
2
I'm not sure I understand your question.
print("Async - Hi Called 1st");
is executed immediately. There is no await
before execution reaches that line. The Future
returned from print1stAsync
is completed when every await
ed Future
in print1stAsync
is completed. If it is not awaited, code in main
continues to execute async results in printAsync()
are being waited for.– Günter Zöchbauer
Nov 11 at 15:15
I'm not sure I understand your question.
print("Async - Hi Called 1st");
is executed immediately. There is no await
before execution reaches that line. The Future
returned from print1stAsync
is completed when every await
ed Future
in print1stAsync
is completed. If it is not awaited, code in main
continues to execute async results in printAsync()
are being waited for.– Günter Zöchbauer
Nov 11 at 15:15
2
2
await callAsync();
means code below that line within the same function (like print(test);
in your example) will be delayed. It doesn't say anything about code in callAsync()
or code that calls print1stAsync
.– Günter Zöchbauer
Nov 11 at 15:46
await callAsync();
means code below that line within the same function (like print(test);
in your example) will be delayed. It doesn't say anything about code in callAsync()
or code that calls print1stAsync
.– Günter Zöchbauer
Nov 11 at 15:46
1
1
I understand it perfectly now, That was the magic sentence " await doesn't say anything about code in callAsync()". Thank you very much.
– Yousef Gamal
Nov 11 at 15:59
I understand it perfectly now, That was the magic sentence " await doesn't say anything about code in callAsync()". Thank you very much.
– Yousef Gamal
Nov 11 at 15:59
|
show 2 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53249859%2fasync-await-not-waiting-before-future-dart%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown