Dart2JS Cannot get async code to run when converted from Dart to Javascript
Asked Answered
T

0

6
main() async {
  print('Hello World');
  await getAsyncString().then((hello) => print(hello));
}

Future<String> getAsyncString() async {
  return 'Hello Async World';
}

This is my code example, index.dart. It is in my web folder.

I run webdev build to compile to js.

When I run dart web/index.dart in my web folder I get the expected output of

Hello World
Hello Async World

When I run node build/index.dart.js or index.js I only get

Hello World

Is the async code running and is this expected behavior?

The full output is too long to post.

Steps to reproduce:

touch testcompile.dart

add the code

dart2js -O2 -o index.js testcompile.dart

node index.js

Trojan answered 5/4, 2019 at 21:45 Comment(2)
If you can provide the generated Javascript code from your index.dart.js, it may be helpful to people attempting to answer the question.Grazia
The output is too long to place in the question. It is relatively easy to replicate though. For anyone interested, install the dart sdk dartlang.org/tools/sdk touch testcompile.dart, add the code, dart2js -O2 -o index.js testcompile.dart, finally node index.jsTrojan

© 2022 - 2024 — McMap. All rights reserved.