Returns a PendingOption with None if this option resolves to
None, otherwise returns a PendingOption with x
.
This is the asynchronous version of and.
x
is a Promise
and rejects, None is returned.const x = pendingOption(some(2));
const y = pendingOption(none<number>());
expect(await x.and(some(3))).toStrictEqual(some(3));
expect(await x.and(Promise.resolve(some(3)))).toStrictEqual(some(3));
expect(await x.and(none())).toStrictEqual(none());
expect(await x.and(Promise.resolve(none()))).toStrictEqual(none());
expect(await y.and(some(3))).toStrictEqual(none());
expect(await y.and(Promise.resolve(none()))).toStrictEqual(none());
Returns a PendingOption with None if this Option resolves
to None, otherwise applies f
to the resolved value and returns the result.
This is the asynchronous version of andThen.
f
rejects or throws, None is returned.const x = pendingOption(some(2));
const y = pendingOption(none<number>());
expect(await x.andThen(n => some(n * 2))).toStrictEqual(some(4));
expect(await x.andThen(n => Promise.resolve(some(n * 2)))).toStrictEqual(some(4));
expect(await x.andThen(_ => none())).toStrictEqual(none());
expect(await y.andThen(n => some(n * 2))).toStrictEqual(none());
Returns a PendingOption with None if this option resolves to
None, otherwise calls f
with the resolved value and returns
a PendingOption with the original value if f
resolves to true
,
or None otherwise.
This is the asynchronous version of filter.
f
rejects or throws, None is returned.const x = pendingOption(some(2));
const y = pendingOption(none<number>());
expect(await x.filter(n => n > 0)).toStrictEqual(some(2));
expect(await x.filter(n => Promise.resolve(n < 0))).toStrictEqual(none());
expect(await y.filter(n => true)).toStrictEqual(none());
Flattens a PendingOption of a PendingOption or Option, resolving nested pending states.
This is the asynchronous version of flatten.
Promise
and rejects,
flattened PendingOption with None is returned.const option1: PendingOption<Option<number>> = getPendingOption();
option1.flatten(); // PendingOption<number>
const option2: PendingOption<PendingOption<number>> = getPendingOption();
option2.flatten(); // PendingOption<number>
const option3: PendingOption<PendingOption<PendingOption<number>>> = getPendingOption();
option3.flatten(); // PendingOption<Option<number>>
Calls f
with the resolved value if this option is Some, then returns this
PendingOption unchanged. Useful for side effects.
This is the asynchronous version of inspect.
const x = pendingOption(some(2));
const y = pendingOption(none<number>());
let sideEffect = 0;
expect(await x.inspect(n => (sideEffect = n))).toStrictEqual(some(2));
expect(sideEffect).toBe(2);
expect(await y.inspect(n => (sideEffect = n))).toStrictEqual(none());
expect(sideEffect).toBe(2); // Unchanged
Returns an async iterator over this pending option’s value, yielding it if it resolves to Some or nothing if it resolves to None.
for await...of
loops and async spread operators (with caution).const x = some(42).toPending();
const y = none<number>().toPending();
const iterX = x.iter();
expect(await iterX.next()).toEqual({ value: 42, done: false });
expect(await iterX.next()).toEqual({ done: true });
const iterY = y.iter();
expect(await iterY.next()).toEqual({ done: true });
async function collect(iter) {
const result = [];
for await (const val of iter) result.push(val);
return result;
}
expect(await collect(x.iter())).toEqual([42]);
expect(await collect(y.iter())).toEqual([]);
Maps the resolved value with f
, returning a PendingOption with the
result if Some, or None if None.
This is the async version of map.
f
throws or rejects, returns None.const x = pendingOption(some(2));
const y = pendingOption(none<number>());
expect(await x.map(n => n * 2)).toStrictEqual(some(4));
expect(await x.map(n => Promise.resolve(n * 2))).toStrictEqual(some(4));
expect(await y.map(n => n * 2)).toStrictEqual(none());
Maps this option by applying a callback to its full state, executing the callback for both Some and None, returning a new PendingOption.
Unlike andThen, which only invokes the callback for Some,
this method always calls f
, passing the entire Option as its argument.
f
throws or returns a Promise
that rejects, the newly
created PendingOption will resolve to a None.const someOpt = pendingOption(some(42));
const noneOpt = pendingOption(none<number>());
const someMapped = someOpt.mapAll(opt => Promise.resolve(some(opt.unwrapOr(0))));
expect(await someMapped).toStrictEqual(some(42));
const noneMapped = noneOpt.mapAll(opt => Promise.resolve(some(opt.unwrapOr(0) + 1)));
expect(await noneMapped).toStrictEqual(some(1));
Matches the resolved option, returning f
applied to the value if Some,
or g
if None. Returns a Promise
with the result.
This is the asynchronous version of match.
f
or g
throws an exception or rejects,
original error will be set as OptionError.reason.f
or g
throws or returns a rejected Promise
, the returned promise
rejects with the original error. In this case the caller is responsible
for handling the rejection.const x = pendingOption(some(2));
const y = pendingOption(none<number>());
expect(await x.match(n => n * 2, () => 0)).toBe(4);
expect(await y.match(n => n * 2, () => 0)).toBe(0);
await expect(y.match(n => n * 2, () => { throw new Error() })).rejects.toThrow(OptionError);
Converts to a PendingResult, using y
as the error value if this
PendingOption resolves to None.
This is the asynchronous version of okOr, check it for more details.
const x = pendingOption(some(2));
const y = pendingOption(none<number>());
expect(await x.okOr("error")).toStrictEqual(ok(2));
expect(await y.okOr("error")).toStrictEqual(err("error"));
Converts to a PendingResult, using the result of mkErr
as the error value if this resolves to None.
This is the asynchronous version of okOrElse.
const x = pendingOption(some(2));
const y = pendingOption(none<number>());
expect(await x.okOrElse(() => "error")).toStrictEqual(ok(2));
expect(await y.okOrElse(() => Promise.resolve("error"))).toStrictEqual(err("error"));
Returns this PendingOption if it resolves to Some, otherwise
returns a PendingOption with x
.
This is the asynchronous version of or.
x
is a Promise
that rejects, None is returned.const x = pendingOption(some(2));
const y = pendingOption(none<number>());
expect(await x.or(some(3))).toStrictEqual(some(2));
expect(await x.or(Promise.resolve(none()))).toStrictEqual(some(2));
expect(await y.or(some(3))).toStrictEqual(some(3));
expect(await y.or(Promise.resolve(none()))).toStrictEqual(none());
Returns this PendingOption if it resolves to Some, otherwise
returns a PendingOption with the result of f
.
This is the asynchronous version of orElse.
f
throws or rejects, None is returned.const x = pendingOption(some(2));
const y = pendingOption(none<number>());
expect(await x.orElse(() => some(3))).toStrictEqual(some(2));
expect(await y.orElse(() => Promise.resolve(some(3)))).toStrictEqual(some(3));
expect(await y.orElse(() => none())).toStrictEqual(none());
Executes f
with the resolved option, then returns a new PendingOption
unchanged.
This is the asynchronous version of tap.
f
throws or rejects, the error is ignored.f
returns a promise, the promise is not awaited before returning.const x = pendingOption(some(42));
const y = pendingOption(none<number>());
let log = "";
expect(await x.tap(opt => (log = opt.toString()))).toStrictEqual(some(42));
expect(log).toBe("Some { 42 }");
expect(await y.tap(opt => (log = opt.toString()))).toStrictEqual(none());
expect(log).toBe("None");
Transposes a PendingOption of a Result into a PendingResult containing an Option.
This is the asynchronous version of transpose.
const x = pendingOption(none<Result<number, string>>());
const y = pendingOption(some<Result<number, string>>(ok(2)));
const z = pendingOption(some<Result<number, string>>(err("error")));
expect(await x.transpose()).toStrictEqual(ok(none()));
expect(await y.transpose()).toStrictEqual(ok(some(2)));
expect(await z.transpose()).toStrictEqual(err("error"));
Returns a PendingOption with Some if exactly one of this option or
y
resolves to Some, otherwise returns a PendingOption with
None.
This is the asynchronous version of xor.
y
is a Promise
that rejects, None is returned.const x = pendingOption(some(2));
const y = pendingOption(none<number>());
expect(await x.xor(some(3))).toStrictEqual(none());
expect(await x.xor(Promise.resolve(none()))).toStrictEqual(some(2));
expect(await y.xor(some(3))).toStrictEqual(some(3));
expect(await y.xor(Promise.resolve(none()))).toStrictEqual(none());
Interface defining an asynchronous Option that wraps a
Promise
resolving to an Option.Extends Option functionality for pending states, with methods mirroring their synchronous counterparts but returning PendingOption or
Promise
for async operations. Rejections typically resolve to None unless otherwise specified.