Created with <3 with dartpad.dev.
Last active
September 1, 2023 22:53
-
-
Save catalunha/6e8e1e17447e3d158f680d3f68efd333 to your computer and use it in GitHub Desktop.
Either
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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