perform<T> static method
Cmd
perform<T>(
- Future<
T> task(), { - required Msg onSuccess(
- T result
- Msg onError(
- Object error,
- StackTrace stack
A command that runs an async function and maps the result to a message.
This is a convenience for wrapping arbitrary async work.
Cmd.perform(
() => httpClient.get(url),
onSuccess: (response) => DataMsg(response.body),
onError: (error) => ErrorMsg(error.toString()),
);
Implementation
static Cmd perform<T>(
Future<T> Function() task, {
required Msg Function(T result) onSuccess,
Msg Function(Object error, StackTrace stack)? onError,
}) {
return Cmd(() async {
try {
final result = await task();
return onSuccess(result);
} catch (e, stack) {
if (onError != null) {
return onError(e, stack);
}
rethrow;
}
});
}