Skip to content

Instantly share code, notes, and snippets.

@matanlurey
Last active February 22, 2017 20:40
Show Gist options
  • Save matanlurey/41854515caad364dfce7669663b9e52d to your computer and use it in GitHub Desktop.
Save matanlurey/41854515caad364dfce7669663b9e52d to your computer and use it in GitHub Desktop.
An example of an unnecessary Completer usage in Dart
import 'dart:async';
final messageStorage = <String>[];
Future save(String message) {
var completer = new Completer();
new Future(() => messagesStorage.add(message)).then((_) => completer.complete());
return completer.future;
}
Future<List<String>> getAll() {
var completer = new Completer();
new Future(() => messagesStorage).then((List<String> messages) => completer.complete(messages));
return completer.future;
}
Future<bool> doServe() async {
var c = new Completer();
var httpServer = await HttpServer.bind(_SVhost, _SVport);
// Connection monitor timer
Timer monitor = new Timer.periodic(new Duration(seconds: 5), (_) {
var connInfo = httpServer.connectionsInfo();
log("Connections[${connInfo.total}]: active[${connInfo.active}] idle[${connInfo.idle}] closing[${connInfo.closing}]");
});
httpServer.listen((request) {
log("Connection from [${request.connectionInfo.remoteAddress.toString()}] at [${request.connectionInfo.remotePort}]");
request.response.writeln("Hello World!");
request.response.close();
c.complete(true);
},
onError: (e) {
log.e("Error occured: ${e}");
}
);
return c.future;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment