Skip to content

Instantly share code, notes, and snippets.

@catalunha
Last active September 1, 2023 22:53
Show Gist options
  • Save catalunha/6e8e1e17447e3d158f680d3f68efd333 to your computer and use it in GitHub Desktop.
Save catalunha/6e8e1e17447e3d158f680d3f68efd333 to your computer and use it in GitHub Desktop.
Either
void main() async {
await getAll();
}
Future<void> getAll() async {
final <Either>[name, value] = await Future.wait([
getName(),
getIdade(),
]);
switch ((name, value)) {
case (Success(value: final name), Success(value: final idade)):
print('Success: Nome: $name, idade: $idade');
case _:
print('Um deles falhou.');
}
}
Future<Either<Exception, String>> getName() async {
return Future.delayed(const Duration(seconds: 4), () => Success('Lucas'));
}
Future<Either<Exception, int>> getIdade() async {
// return Future.delayed(const Duration(seconds: 1), () => Success(13));
return Failure(Exception('falha em getIdade'));
}
sealed class Either<E extends Exception, S> {}
class Success<E extends Exception, S> extends Either<E, S> {
final S value;
Success(this.value);
}
class Failure<E extends Exception, S> extends Either<E, S> {
final E exception;
Failure(this.exception);
}
class Nil {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment