async<T> static method
Creates a context for async early return, similar to "Do notation". Works like the Rust "?" operator, which is a
"Early Return Operator". Here "$" is used as the "Early Return Key". when "$" is used on a type _None
,
immediately the context that "$" belongs to is returned with None(). e.g.
FutureOption<int> intNone() async => const None();
FutureOption<int> earlyReturn(int val) => Option.async(($) async {
int x = await intNone()[$]; // returns [None] immediately
return Some(x + 3);
});
expect(await earlyReturn(2), const None());
This should be used at the top level of a function as above. Passing "$" to any other functions, nesting, or attempting to bring "$" out of the original scope should be avoided.
Implementation
@pragma("vm:prefer-inline")
static Future<Option<T>> async<T>(
_OptionAsyncEarlyReturnFunction<T> fn) async {
try {
return await fn(const _OptionEarlyReturnKey._());
} on _OptionEarlyReturnNotification catch (_) {
return Future.value(None);
}
}