Dart language learning-asynchronous programming async and await

Preface

DartIt is a computer programming language developed by Google. It is used for webthe development of servers, mobile applications and other fields . It was Dartunveiled in 2011. At the DartDeveloper Summit in May 2015 , it unveiled a Dartlanguage-based mobile application development framework Sky, which was later renamedFlutter

async and await

Earlier, we used Futureto add time to the micro task queue and events to execute asynchronously, and get the results of asynchronous execution for the registered callback. The keywords asyncand awaitthe Dartlanguage support asynchronous methods. You can use them without using Futurethem. Write asynchronous code

  • asyncThe decorated function is an asynchronous function, which means that there is a time-consuming operation in this function and needs to be processed asynchronously
  • awaitCan only be used in asynchronous functions, used to modify the part of the time-consuming operation, this part will be added to the event queue, which means that it will wait for the asynchronous method to execute
  • awaitRepresents waiting, which will block the subsequent execution of the asynchronous function. When awaitthe Futureexecution of the reference is completed, the execution will awaitbegin later

Case 1

Print the startup time, load the picture, and print the startup completion time when the program starts

  • The printing start time does not need to be time-consuming, so it is synchronous
  • Loading images takes time, so it needs to be asynchronous
  • Initializing certain parameter configurations does not require time-consuming, so it is synchronous

First we create the following method

main()
printBootTime()
loadImg()
init()

We need to load pictures asynchronously, without affecting other startup processes

main() {
 printBootTime();
 loadImg(); // The asynchronous function is called here without using await because it does not affect the rest of the startup process.
 init();
}

printBootTime(){
 print("Start time"+DateTime.now().millisecondsSinceEpoch.toString());
}

loadImg() async{
  print("Start loading images");
  await Future.delayed(new Duration(seconds: 3));
  print("Image loading completed");
}

init(){
  print("Initialization parameters");
}

// Print results
// Start time 1624708372396
// Start loading images
// Initialization parameters
// After 3 seconds
// Image loading completed 

Case 2

We need to load the image asynchronously, and load the network data after the loading is completed, without affecting other startup processes.

main() {
 printBootTime();
 loadImg();
 init();
}

printBootTime(){
 print("Start time"+DateTime.now().millisecondsSinceEpoch.toString());
}

loadImg() async{
  print("Start loading images");
  await Future.delayed(new Duration(seconds: 3));
  print("Image loading completed");
  await loadNetworkData();
}

loadNetworkData() async{
  print("Start loading network data");
  await Future.delayed(new Duration(seconds: 2));
  print("Network data loading completed");
}

init(){
  print("Initialization parameters");
}

// Print results
// Start time 1624708462181
// Start loading images
// Initialization parameters
// After 3 seconds
// Image loading completed
// Start loading network data
// After 2 seconds
// Network data loading is complete

Case 3

Use HttpClient to retrieve network data and print the results.

main() async{
  HttpClientResponse res = await getBaiduData();// Since you need to wait for the network request responder, you need to await 
  print(res.statusCode);
}

getBaiduData() async{
  HttpClient client = HttpClient();
  // client.getUrl returns a Future
  var request =await client.getUrl(Uri.http("www.baidu.com", ""));
  // request.close returns a Future
  var response = await request.close();
  return response;
}

// 200

Leave a Reply