MaybeAsync
The MaybeAsync type in PureEval extends the Maybe type to support asynchronous operations. Like Maybe, it's a fusion of Data Maybe and Class Monad but with asynchronous capabilities.
MaybeAsync
The MaybeAsync type encapsulates a value that may be an indeterminate Promise.
Both undefined and null are considered as NothingAsync.
- Type
MaybeAsync.of()
Lifts a Promise into a MaybeAsync type.
- Type
- Example
const foo = new Promise((res, rej) => {
res('Hello, World!');
});
const task = MaybeAsync.of(foo);const foo = new Promise((res, rej) => {
res('Hello, World!');
});
const task = MaybeAsync.of(foo);MaybeAsync.is()
Determines whether a value is of type MaybeAsync.
- Type
- Example
MaybeAsync.is(JustAsync(new Promise((res) => res(1)))); // true
MaybeAsync.is(NothingAsync); // trueMaybeAsync.is(JustAsync(new Promise((res) => res(1)))); // true
MaybeAsync.is(NothingAsync); // trueMaybeAsync.isNothing()
Determines if the MaybeAsync is Nothing.
- Type
- Example
JustAsync(1).isNothing(); // false
JustAsync(null).isNothing(); // trueJustAsync(1).isNothing(); // false
JustAsync(null).isNothing(); // trueMaybeAsync.map()
Maps the value inside MaybeAsync through a function, constructing a new MaybeAsync to hold the result.
- Type
- Example
JustAsync(new Promise((res) => res('Hello, World!'))).map(toLowerCase); // MaybeAsync "hello, world!"JustAsync(new Promise((res) => res('Hello, World!'))).map(toLowerCase); // MaybeAsync "hello, world!"MaybeAsync.fold()
WARNING
Note: Due to the asynchronous nature, the return value is a Promise. Use await to resolve it.
Applies a function to the value within MaybeAsync a.
- Type
- Example
await JustAsync(new Promise((res) => res('Hello, World!'))).fold(() => 'fuck', id); // "Hello, World!"
await JustAsync(new Promise((res) => res(null))).fold(() => 'fuck', id); // "fuck"await JustAsync(new Promise((res) => res('Hello, World!'))).fold(() => 'fuck', id); // "Hello, World!"
await JustAsync(new Promise((res) => res(null))).fold(() => 'fuck', id); // "fuck"MaybeAsync.chain()
Chains a function that returns a MaybeAsync type, allowing for method chaining.
- Type
- Example
// Demonstrative example with asynchronous operations
// [...]
sendEmailToUserById(123);// Demonstrative example with asynchronous operations
// [...]
sendEmailToUserById(123);JustAsync()
Constructor function for the MaybeAsync type.
Equivalent to MaybeAsync.of.
- Type
- Example
JustAsync(1); // Just 1JustAsync(1); // Just 1NothingAsync
Constructor for the MaybeAsync type, representing the absence of a value.