[Dart] learning - async Dart sum await

I. Overview

He joined in Dart1.9 the async and await keywords, keywords with these two, we can be more concise to write asynchronous code without the need to call the Future related API. They allow you like to write synchronization code, like writing asynchronous code and do not need to use the Future interface. Future corresponding to another encapsulation are associated API interface is provided a method of operation easier for the API Future

     The  async  keyword is used as a suffix method declaration, have the following meanings

  • A modified method will  Future  object as the return value
  • This method synchronization code means wherein the execution until the first await a keyword , and then suspended it to other parts of the method;
  • Once the  await  references keywords  Future  task execution is completed, we await the next line of code will be executed immediately.

Two, Async / await example resolves

  • Example 1:
    Use Async/awaitis asynchronous operation may be implemented directly on the following example:
    main() {
      create();
    }
    void create(){
       String data =  getData();
       print(data);
       print("I love Future");
    }
    getData() async{
      return await "I love Android";
    }

    Run the above code, incorrect report:

    type 'Future<dynamic>' is not a subtype of type 'String'

    The report does not match the type? why? After a search, they found getDataan asynchronous operation function, its return value is a awaitresult of delayed execution. In Dart, there are awaitmarked calculation, the resulting value is a Futuretarget, Futureis not of type String , on the error. So how did the correct result of the asynchronous get it? Dart marks the provisions of async function can only be called by the await , so the following change:

    main() {
      create();
    }
    
    void create() async{
       String data =  await getData();
       print(data);
       print("I love Future");
    }
    getData() async{
    return await "I love Android"; }

    The following direct remove asyncfunction packaging, directly in the getDatamethod in to datacarry out the assignment:

    String data;
    main() {
      create();
    }
    void create(){ getData(); print("I love Future"); }
    getData() async{ data
    = await "I love Android"; print(data); }

    The above output is:

    I love Future
    I love Android

    Can be found, the first output is I love Futurethen back output I love Android, can be found when the function is asyncwhen modified, it will go to perform the following operations, the following operations when executed, then execution is asyncmodified method. asyncUsed to indicate the function is asynchronous, defined function returns an Futureobject awaitfollowed by a Futureshowing wait for the asynchronous task is completed, the asynchronous completion will go down. To note the following:

    1. await async keyword must be used within a function, that is, without adding await async error.
    2. Async function call must await keyword, add async if not add will await the order to execute code.
  • Example Two:
    main() {
      _startMethod();
      _method_C();
    
    }
    
    _startMethod() async{
      _method_A();
      await _method_B();
      print("start结束");
    }
    _method_A () { 
      Print ( "A starts executing this method ~" ); 
    
    } 
    
    _method_B () the async { 
      Print ( "B starts executing this method ~" ); 
      the await Print ( "execute later phrase ~" ); 
      Print ( " continue phrase ha ~ 11111 " ); 
    } 
    
    _method_C () { 
      Print ( " start C " ); 
    }

    The results are as follows:

    A starts executing this method ~ 
    B starts executing this method ~ 
    executed later phrase ~ 
    C starts 
    continue phrase ha 11111 ~ 
    Start End

  过程分析:

    1. When async as a method name suffix statement that returns the value of this method is a Future ;
    2. When the implementation of the method code with await time marked keyword, it will suspend the process performed in other part;
    3. When await keyword refers to the Future executed, the next line of code will be executed immediately.
也就是首先执行_startMethod这个方法用async声明了,因为方法里调用了_method_A,所以先输出print("A开始执行这个方法~");,后面执行_method_B(),这个方法用await关键字声明,所以会暂停print("start结束");的执行,然后继续执行_method_B()print("B开始执行这个方法~");输出,下一行遇到await关键字,会暂停其他代码的执行。当await关键字引用的Future执行完成(也就是执行print("后面执行这句话~"),_method_C()方法会立即执行,然后执行继续执行这句哈11111~,最后执行print("start结束"); 



 
  
 


 

 

Guess you like

Origin www.cnblogs.com/lxlx1798/p/11131674.html