i have question about asynchronous programming at flutter

54 Views Asked by At
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?

1

There are 1 best solutions below

0
Khyati Modi On

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.