Dart async/await encapsulation -
i want return fractional of decimal, if function takes time, function must give up. tried this, doesn't work... did wrong. show me mistake?
string decimaltofractional(double d) async { var df = 1.0; var top = 1; var bot = 1; var future = new future<string>(() { while (df != d) { if (df < d) { top += 1; } else { bot += 1; top = (d * bot).toint(); } df = top / bot; } return new future.value('$top/$bot'); }); future.timeout(new duration(seconds: 2), ontimeout: () => new future.value(d.tostring())); return await future; }
there several problems code. if want return result of async operation return type has future<...>
future<string> decimaltofractional(double d) async {
you can consume result
main() async { print(await decimaltofractional(123456789.123456789)); }
if want timeout take effect need return event loop. long sync execution running timeout doesn't fire. see https://stackoverflow.com/a/22473556/217408
Comments
Post a Comment