0

Trying to figure a good way to handle nested async functions I'm doing some tests with dartpad. In the following example I don't understand why _Future is printed instead of the value (level1String) ?
The output is :
toto
before await level2
before duration
after duration
13
after level 2
Instance of '_Future' // my concern is here since "after level 2" is printed I should have reached the return statement
after level 1
yeah!

import 'dart:convert';
import "dart:async";
import "dart:html";

void main() async {
  print('toto');
  await print(level1String());
  print('after level 1');
}

Future<String> level1String () async {
  print('before await level2');
   print(level2String());
  print('after level 2');
  return 'level1String';
}

int level2String () {
  print('before duration');
  Timer(Duration(seconds: 3), () {
    print('yeah!');
  });
  print('after duration');
  return 13;
}

1 Answer 1

2
await print(level1String());

should be

print(await level1String());

level1String() is returning the Future you need to await, not print()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.