简体   繁体   中英

i have question about asynchronous programming at flutter

void main() async {
  check();
  print('end');
}

Future check() async {
  var version = lookUpVersion();
  print(version);
}

int lookUpVersion() {
  return 12;
}
void main() async {
  check();
  print('end');
}

Future check() async {
  var verion = await lookUpVersion();

  print(version);
}

int lookUpVersion() {
  return 12;
}

These two code only have one difference, await keyword.

I wonder that why not did they wait for main function code? even I used Future+async keyword at first code.

Can you explain about this?

The async and await keywords provide a declarative way to define asynchronous functions and use their results.

For first one - result will be

//12
//end

For second one - result will be

//end
//12

Which means if you add await it will become asynchronous.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM