Rust-like interface Option class.
const v = Option.fromNullish(
await fetchSomething(), /* which returns { something: number } | null */
);
const maybeSomething = v.map((e) => e.something);
const unwrapped = maybeSomething.unwrapOr(Number.NaN);Use .andThen() instead.
We may want to something like following intuitively in Rust.
match v {
Some(v) => {
v.work.await;
}
_ => {}
}But yes, that couldn't be possible even if using .match(). Instead you can do
that as following.
await v.andThen(async (v) => await v.work()).awaited();The method .awaited() converts from Option<Promise<T>> into
Promise<Option<T>>.