Usually the error type.
Usually the success value type.
Let yield* bind this Either inside a do block: yields this Either and
returns the Right value the block is resumed with.
It exists for yield*, not for spreading: [...either] is a one-element
array holding this Either itself.
A generator that yields this Either once.
Map both sides at once: onLeft for a Left, onRight for a Right. Only
the function for this side is called.
If the function called throws, the error propagates from this call; it is
not captured as a Left.
This is Haskell's bimap (Data.Bifunctor). x.bimap(f, g) is
x.mapLeft(f).map(g).
The new Left type.
The new Right type.
A Left holding onLeft(value), or a Right holding
onRight(value).
Chain a step that may itself fail; the first Left short-circuits.
If f throws, the error propagates from this call; it is not captured as a
Left (use tryCatch for that).
This is Haskell's >>=. f must return the same Left type, so every step
in a chain shares one error type.
The new Right type.
The Either f returns, or this Left.
Recover from a Left: handler gets the Left value and its Either replaces
this one. A Right passes through without calling handler.
If handler throws, the error propagates from this call; it is not
captured as a Left.
This is Haskell's catchError (MonadError). The handler may change the
Left type, but not the Right type: it returns Either<L2, R>.
The Left type after recovery.
The Either handler returns, or this Right.
Collapse the Either into one value by handling both branches.
If onLeft or onRight throws, the error propagates from this call.
This is Haskell's either, renamed after the common JavaScript name; the
Left handler comes first, as in Haskell.
The type both handlers return.
What the handler for this branch returns.
Extract the Right value or provide a default derived from the Left.
If fallback throws, the error propagates from this call.
This is Haskell's fromRight, but it takes a function of the Left rather
than a plain default, so the fallback can use the error and is only
computed for a Left.
The Right value, or fallback(left).
Apply f to the Left value; a Right passes through unchanged.
If f throws, the error propagates from this call; it is not captured as a
Left.
This is Haskell's first (Data.Bifunctor), renamed because first doesn't
say which side it maps.
The new Left type.
A Left holding f(value), or this Right.
Pattern matching. If a handler throws, the error propagates from this call.
Haskell has no counterpart; it uses case or either. Taking a handlers
object names each branch at the call site, and the handlers may return
different types.
A left handler for a Left and a right handler for a
Right.
What the handler for this branch returns.
Run a side effect with the Right value, keeping this Either as it is; a
Left passes through without calling f.
If f throws, the error propagates from this call; it is not captured as a
Left.
Haskell has no counterpart; the name matches IO#tap and AsyncIO#tap.
The side effect, given the Right value; its return is ignored.
This Either.
StaticdoRun a do block over Either: Haskell's do notation, with x <- m
written const x = yield* m.
Each yield* m binds the Right value of m, typed with no annotation. The
first Left stops the block there, and the result is that Left. The block
runs at once, so a throw in it propagates from this call. A plain yield
(without *) binds unknown; use yield*.
The result's Left type is the union of the Left types of every Either the block yields.
The Eithers the block yields; inferred.
The value the block returns.
A Right holding what the block returns, or the first Left.
import { Either } from '@b-jones-rfd/func-help'
const parsePort = (s: string): Either<string, number> => {
const n = Number(s)
return Number.isInteger(n) ? Either.right(n) : Either.left(`bad port ${s}`)
}
const lookupHost = (port: number): Either<Error, string> =>
port === 80 ? Either.right('example.com') : Either.left(new Error('no host'))
const address = Either.do(function* () {
const port = yield* parsePort('80') // number
const host = yield* lookupHost(port) // string
return `${host}:${port}`
}) // Either<string | Error, string>, Right('example.com:80')
StaticfromConvert a nullable/undefinable value into an Either, using the provided
onNull thunk to create a Left value when the input is null or undefined.
If onNull throws, the error propagates from this call.
onNull is a thunk so the Left is only built when it is needed.
The Left type onNull returns.
The non-null value type.
A Right holding value, or a Left holding onNull().
StaticleftWrap a value as a Left, Haskell's Left.
R defaults to never, so name it when the Left must sit beside Rights
of a known type, such as in a variable or array.
The Left type.
The Right type; defaults to never.
The Left value, usually an error.
A Left holding value.
StaticofLift a value into a Right with no Left type.
This is Haskell's pure (equivalently Right); the Left type is fixed at
never, so use Either.right when the chain needs a Left type.
The Right type.
The Right value.
A Right holding value, typed Either<never, R>.
StaticrightWrap a value as a Right, Haskell's Right.
R comes first so it can be inferred from value while L is named
explicitly, as in Either.right<number, string>(1).
The Right type.
The Left type; defaults to never.
The Right value.
A Right holding value.
StaticsequenceTurn an array of Eithers into an Either of an array: a Right holding
every Right value in input order, or the first Left.
An empty array gives Right([]). Nothing in it calls caller code, so it
never throws.
This is Haskell's sequence (Data.Traversable). A tuple gives a tuple:
[Either<string, number>, Either<HostError, string>] gives
Either<string | HostError, [number, string]>, with no as const. An
array gives an array, and the Left is the union of the inputs' Lefts.
The array or tuple of Eithers.
The Eithers, in order.
A Right holding the Right values in input order (a tuple for a tuple), or the first Left.
import { Either } from '@b-jones-rfd/func-help'
const port: Either<string, number> = Either.right(8080)
const host: Either<string, string> = Either.right('localhost')
Either.sequence([host, port]) // Right(['localhost', 8080]), Either<string, [string, number]>
Either.sequence([host, Either.left('no port')]) // Left('no port')
StatictraverseApply f to each item in order and collect the Rights into one array;
the first Left stops it, and f is not called for the items after.
An empty array gives Right([]).
If f throws, the error propagates from this call; it is not captured as a
Left.
This is Haskell's traverse (also mapM). Like Array#map, f also
gets the item's index.
The item type.
The Left type f returns.
The Right type f returns.
A Right holding every Right value in input order, or the first Left.
import { Either } from '@b-jones-rfd/func-help'
const parsePort = (s: string): Either<string, number> =>
/^\d+$/.test(s) ? Either.right(Number(s)) : Either.left(`'${s}' is not a port`)
Either.traverse(['80', '443'], parsePort) // Right([80, 443])
Either.traverse(['80', 'http', 'x'], parsePort) // Left("'http' is not a port")
StatictryRun effect, returning its result as a Right, or what it throws as a Left.
Without onError the Left is the thrown value, typed unknown.
Haskell has no counterpart: it is the bridge from code that throws into
Either. Only a throw from effect is captured.
The type effect returns.
The code that may throw.
A Right holding the result, or a Left holding the thrown value.
Run effect, returning its result as a Right, or what it throws mapped
through onError as a Left.
Haskell has no counterpart: it is the bridge from code that throws into
Either, and onError gives the Left a type in place of unknown.
Only a throw from effect is captured; if onError throws, the error
propagates from this call.
The type effect returns.
The Left type onError returns.
A Right holding the result, or a Left holding onError(thrown).
A value that is either a Left (typically an error) or a Right (a
successful computation).
Either<L, R> ≅ Left | Right
Only the Right branch is considered for
map/bindsequencing; the Left branch short‑circuits the chain.A throw in a caller-supplied function propagates synchronously from the call that ran it and is not captured as a Left; only
tryCatchcaptures throws, and only fromeffect.Example