Is there a way to block until an async function completes it's future and return the value of the future?
Future<String> myAsyncFunction(String input) {
..
}
content.replaceAllMapped(MY_REGEX, (match) {
return $make_sync(myAsyncFunction(match.group(0)));
});
What would $make_sync look like? Future.sync() doesn't look helpful here.
What you are asking for requires some kind of co-routines, where you can suspend normal code, and return to it later. Dart doesn't have that functionality, it uses asynchronous functions instead.
As an example I would like to use an async function within a callback to replaceAllMapped:
Future<String> myAsyncFunction(String input) {
..
}
content.replaceAllMapped(MY_REGEX, (match) {
return $make_sync(myAsyncFunction(match.group(0)));
});
What would $make_sync look like? Future.sync() doesn't look helpful here.
It does not exist, and currently can't exist.It is a good argument for adding coroutines though - replaceAllMapped is not a function that we would make an async version of, but that restricts its callback to being synchronous.
What you are asking for requires some kind of co-routines, where you can suspend normal code, and return to it later. Dart doesn't have that functionality, it uses asynchronous functions instead.
I am wondering how File.reasAsStringSync() is implemented.
It is a good argument for adding coroutines though - replaceAllMapped is not a function that we would make an async version of, but that restricts its callback to being synchronous.
non-orthogonal, to say the least..
Problem would go away if auto-await for Futures was implemented by compiler/runtime by default.. Otherwise, it's a puzzle. Introducing more keywords and more concepts doesn't make the puzzle disappear.
--
For other discussions, see https://groups.google.com/a/dartlang.org/
For HOWTO questions, visit http://stackoverflow.com/tags/dart
To file a bug report or feature request, go to http://www.dartbug.com/new
To unsubscribe from this group and stop receiving emails from it, send an email to misc+uns...@dartlang.org.
library main;
import 'dart:async';import 'package:vane/vane.dart';
class Test extends Vane { var pipeline = [ProcessData1, This];
@Route("/") Future test([String name = ""]) { // Print start message for easier understanding of flow print("Start of test, pipeline index: ${pIndex}"); writeln("Start of test, pipeline index: ${pIndex}");
// Receive value on the tube var value = tube.receive();
// Print value print("Our msg: ${value}"); writeln("Our msg: ${value}");
return close(); }}
class ProcessData1 extends Vane { Future main() { print("Start of ProcessData1, pipeline index: ${pIndex}"); writeln("Start of ProcessData1, pipeline index: ${pIndex}");
// replaceAllMapped() example from dartlang.org pigLatin(String words) => words.replaceAllMapped( new RegExp(r'\b(\w*?)([aeiou]\w*)', caseSensitive: false), (Match m) => "${m[2]}${m[1]}${m[1].isEmpty ? 'way' : 'ay'}");
// Do something async, we use a Timer here to simulate any asynchronous operation new Timer(new Duration(seconds: 3), () { print("Inside ProcessData1 Timer, doing async work..."); writeln("Inside ProcessData1 Timer, doing async work...");
var msg = pigLatin('I have a secret now!');
tube.send(msg); next(); });
print("End of ProcessData1, pipeline index: ${pIndex}"); writeln("End of ProcessData1, pipeline index: ${pIndex}");
return end; }}
void main() => serve();
Hi George,Just as Lasse said, I have also found (by lots of testing) that this is not possible.
And with each and then, you also get more indentation, and more indentation etc...More example of how to use Vane middleware handlers can be found here: http://www.dartvoid.com/vane/Regards, Robert