Skip to content

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
js
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
js
MaybeAsync.is(JustAsync(new Promise((res) => res(1)))); // true
MaybeAsync.is(NothingAsync); // true
MaybeAsync.is(JustAsync(new Promise((res) => res(1)))); // true
MaybeAsync.is(NothingAsync); // true

MaybeAsync.isNothing()

Determines if the MaybeAsync is Nothing.

  • Type
  • Example
js
JustAsync(1).isNothing(); // false
JustAsync(null).isNothing(); // true
JustAsync(1).isNothing(); // false
JustAsync(null).isNothing(); // true

MaybeAsync.map()

Maps the value inside MaybeAsync through a function, constructing a new MaybeAsync to hold the result.

  • Type
  • Example
js
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
js
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
js
// 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
js
JustAsync(1); // Just 1
JustAsync(1); // Just 1

NothingAsync

Constructor for the MaybeAsync type, representing the absence of a value.

PureEval released under the GPL-3.0 License.