Skip to content

True Myth / maybe / andThen

Function: andThen()

Call Signature

andThen<T, U>(thenFn, maybe): Maybe<U>

Apply a function to the wrapped value if Just and return a new Just containing the resulting value; or return Nothing if Nothing.

This differs from map in that thenFn returns another Maybe. You can use andThen to combine two functions which both create a Maybe from an unwrapped type.

You may find the .then method on an ES6 Promise helpful for comparison: if you have a Promise, you can pass its then method a callback which returns another Promise, and the result will not be a nested promise, but a single Promise. The difference is that Promise#then unwraps all layers to only ever return a single Promise value, whereas Maybe.andThen will not unwrap nested Maybes.

This is sometimes also known as `bind`, but *not* aliased as such

because bind already means something in JavaScript.

Example

(This is a somewhat contrived example, but it serves to show the way the function behaves.)

ts
import Maybe, { andThen, toString } from 'true-myth/maybe';

// string -> Maybe<number>
const toMaybeLength = (s: string) => Maybe.of(s.length);

// Maybe<string>
const aMaybeString = Maybe.of('Hello, there!');

// Maybe<number>
const resultingLength = andThen(toMaybeLength, aMaybeString);
console.log(toString(resultingLength)); // 13

Note that the result is not Just(Just(13)), but Just(13)!

Type Parameters

T

T

The type of the wrapped value.

U

U

The type of the wrapped value in the resulting Maybe.

Parameters

thenFn

(t) => Maybe<U>

The function to apply to the wrapped T if maybe is Just.

maybe

Maybe<T>

The Maybe to evaluate and possibly apply a function to the contents of.

Returns

Maybe<U>

The result of the thenFn (a new Maybe) if maybe is a Just, otherwise Nothing if maybe is a Nothing.

Call Signature

andThen<T, R>(thenFn, maybe): Maybe<ValueFor<R>>

Apply a function to the wrapped value if Just and return a new Just containing the resulting value; or return Nothing if Nothing.

This differs from map in that thenFn returns another Maybe. You can use andThen to combine two functions which both create a Maybe from an unwrapped type.

You may find the .then method on an ES6 Promise helpful for comparison: if you have a Promise, you can pass its then method a callback which returns another Promise, and the result will not be a nested promise, but a single Promise. The difference is that Promise#then unwraps all layers to only ever return a single Promise value, whereas Maybe.andThen will not unwrap nested Maybes.

This is sometimes also known as `bind`, but *not* aliased as such

because bind already means something in JavaScript.

Example

(This is a somewhat contrived example, but it serves to show the way the function behaves.)

ts
import Maybe, { andThen, toString } from 'true-myth/maybe';

// string -> Maybe<number>
const toMaybeLength = (s: string) => Maybe.of(s.length);

// Maybe<string>
const aMaybeString = Maybe.of('Hello, there!');

// Maybe<number>
const resultingLength = andThen(toMaybeLength, aMaybeString);
console.log(toString(resultingLength)); // 13

Note that the result is not Just(Just(13)), but Just(13)!

Type Parameters

T

T

The type of the wrapped value.

R

R extends AnyMaybe

Parameters

thenFn

(t) => R

The function to apply to the wrapped T if maybe is Just.

maybe

Maybe<T>

The Maybe to evaluate and possibly apply a function to the contents of.

Returns

Maybe<ValueFor<R>>

The result of the thenFn (a new Maybe) if maybe is a Just, otherwise Nothing if maybe is a Nothing.

Call Signature

andThen<T, U>(thenFn): (maybe) => Maybe<U>

Apply a function to the wrapped value if Just and return a new Just containing the resulting value; or return Nothing if Nothing.

This differs from map in that thenFn returns another Maybe. You can use andThen to combine two functions which both create a Maybe from an unwrapped type.

You may find the .then method on an ES6 Promise helpful for comparison: if you have a Promise, you can pass its then method a callback which returns another Promise, and the result will not be a nested promise, but a single Promise. The difference is that Promise#then unwraps all layers to only ever return a single Promise value, whereas Maybe.andThen will not unwrap nested Maybes.

This is sometimes also known as `bind`, but *not* aliased as such

because bind already means something in JavaScript.

Example

(This is a somewhat contrived example, but it serves to show the way the function behaves.)

ts
import Maybe, { andThen, toString } from 'true-myth/maybe';

// string -> Maybe<number>
const toMaybeLength = (s: string) => Maybe.of(s.length);

// Maybe<string>
const aMaybeString = Maybe.of('Hello, there!');

// Maybe<number>
const resultingLength = andThen(toMaybeLength, aMaybeString);
console.log(toString(resultingLength)); // 13

Note that the result is not Just(Just(13)), but Just(13)!

Type Parameters

T

T

The type of the wrapped value.

U

U

The type of the wrapped value in the resulting Maybe.

Parameters

thenFn

(t) => Maybe<U>

The function to apply to the wrapped T if maybe is Just.

Returns

The result of the thenFn (a new Maybe) if maybe is a Just, otherwise Nothing if maybe is a Nothing.

(maybe): Maybe<U>

Parameters

maybe

Maybe<T>

Returns

Maybe<U>

Call Signature

andThen<T, R>(thenFn): (maybe) => Maybe<ValueFor<R>>

Apply a function to the wrapped value if Just and return a new Just containing the resulting value; or return Nothing if Nothing.

This differs from map in that thenFn returns another Maybe. You can use andThen to combine two functions which both create a Maybe from an unwrapped type.

You may find the .then method on an ES6 Promise helpful for comparison: if you have a Promise, you can pass its then method a callback which returns another Promise, and the result will not be a nested promise, but a single Promise. The difference is that Promise#then unwraps all layers to only ever return a single Promise value, whereas Maybe.andThen will not unwrap nested Maybes.

This is sometimes also known as `bind`, but *not* aliased as such

because bind already means something in JavaScript.

Example

(This is a somewhat contrived example, but it serves to show the way the function behaves.)

ts
import Maybe, { andThen, toString } from 'true-myth/maybe';

// string -> Maybe<number>
const toMaybeLength = (s: string) => Maybe.of(s.length);

// Maybe<string>
const aMaybeString = Maybe.of('Hello, there!');

// Maybe<number>
const resultingLength = andThen(toMaybeLength, aMaybeString);
console.log(toString(resultingLength)); // 13

Note that the result is not Just(Just(13)), but Just(13)!

Type Parameters

T

T

The type of the wrapped value.

R

R extends AnyMaybe

Parameters

thenFn

(t) => R

The function to apply to the wrapped T if maybe is Just.

Returns

The result of the thenFn (a new Maybe) if maybe is a Just, otherwise Nothing if maybe is a Nothing.

(maybe): Maybe<ValueFor<R>>

Parameters

maybe

Maybe<T>

Returns

Maybe<ValueFor<R>>