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:
Dart Image



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 awaitbut 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?










share|improve this question




























    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:
    Dart Image



    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 awaitbut 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?










    share|improve this question


























      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:
      Dart Image



      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 awaitbut 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?










      share|improve this question















      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:
      Dart Image



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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 14:53

























      asked Nov 11 at 14:48









      Yousef Gamal

      6410




      6410
























          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();






          share|improve this answer



















          • 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








          • 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 no await before execution reaches that line. The Future returned from print1stAsync is completed when every awaited 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




            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




            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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          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

























          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();






          share|improve this answer



















          • 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








          • 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 no await before execution reaches that line. The Future returned from print1stAsync is completed when every awaited 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




            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




            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















          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();






          share|improve this answer



















          • 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








          • 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 no await before execution reaches that line. The Future returned from print1stAsync is completed when every awaited 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




            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




            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













          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();






          share|improve this answer














          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();







          share|improve this answer














          share|improve this answer



          share|improve this answer








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




            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 no await before execution reaches that line. The Future returned from print1stAsync is completed when every awaited 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




            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




            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




            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




            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 no await before execution reaches that line. The Future returned from print1stAsync is completed when every awaited 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




            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




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


















          draft saved

          draft discarded




















































          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.




          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Florida Star v. B. J. F.

          Danny Elfman

          Lugert, Oklahoma