Skip to content

Instantly share code, notes, and snippets.

@miquelbeltran
Created June 20, 2019 04:01
Show Gist options
  • Save miquelbeltran/088bf2e512b622ef10bfcf3959b844b1 to your computer and use it in GitHub Desktop.
Save miquelbeltran/088bf2e512b622ef10bfcf3959b844b1 to your computer and use it in GitHub Desktop.
import "dart:async";
class SynchronousError<T> implements Future<T> {
final Object _error;
SynchronousError(this._error);
@override
Stream<T> asStream() {
final StreamController controller = StreamController<T>();
controller.addError(_error);
controller.close();
return controller.stream;
}
@override
Future<T> catchError(Function onError, {bool test(dynamic error)}) {
onError(_error);
return Completer<T>().future;
}
@override
Future<E> then<E>(dynamic f(T value), {Function onError}) {
return SynchronousError<E>(_error);
}
@override
Future<T> timeout(Duration timeLimit, {dynamic onTimeout()}) {
return Future<T>.error(_error).timeout(timeLimit, onTimeout: onTimeout);
}
@override
Future<T> whenComplete(dynamic action()) {
action();
return this;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment