
Company News
Socket Named Top Sales Organization by RepVue
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.
A fully typed, zero dependency implementation of the functional programming Either object for JavaScript and TypeScript.
Eithers are incredibly useful for functions that may throw an error. Throwing an error breaks the control flow of your program. The Either object is functional programming's solutions to handling functions that may error or succeed (and return a value).
npm i -g ether
import { Left, Right, Either } from "either"
const myFirstEither = Left('uh oh, something broke');
const myOtherEither = Right(42);
const alsoAnEither = Either.of(
'on a roll',
'right'
); // => Equivalent to Right('on a roll')
const stillAnEither = Either.of(
new Error("Cannot read property foo of undefined"),
'left'
); // => Equivalent to Left(new Error("Cannot read property foo of undefined");
When doing console.log(myEither); it's best to do console.log(myEither.toString()). This provides better output. JavaScript does not automatically invoke an object's toString method by default.
For convenience, there is a shorter named equivalent .toStr method.
For even more convenience, there is a .log method which will invoke console.log and the toString method for you.
const myEither = Right("Hello World");
// The below are all equivalent
console.log(myEither.toString());
console.log(myEither.toStr();
myEither.log();
/**
/**
/**
/**
/**
/**
.orElse(fn2ReturnsEither())
.orElse(fn3ReturnsEither())
.orElse(fn4ReturnsEither())
*/ orElse(otherEither: Either): Either<A | B>
/**
flatMap when the provided function does/**
*/ static map<B, A>( fn: (val: A) => B ): ( either: Either ) => Either<A | B>
/**
/**
(a: number) => (b: number) => (c: number) => a + b + c,
Right(18),
Right(4),
Right(6)
(a: number) => (b: { age: number }) => a + b.age,
Right(78),
Right({ age: 22 })
*/ static liftN( // @ts-ignore fn, ...args: [Either, ...Either[]] ): Either
/**
return Math.random() > .5 ?
Right(val => val * 2) :
Left("bad luck today");
*/ ap<B, C>(either: Either): Either<A | B | C>
/**
*/ fold(fnA: (val: A) => B, fnB: (val: A) => B): B
/**
map when the provided function returnsmap,flatMap, and then, then should always work.
*/
flatMap(fn: (val: A) => Either): Either<A | B>/**
*/ static flatMap<B, A>( fn: (val: A) => Either ): (either: Either) => Either<A | B>
/**
Math.random() > .5 ?
Right(val * 2) :
Left("Just not your day :(. Try again next time");
.then(alwaysDouble);
*/ then(fn: (val: A) => B | Either): Either<A | B>
/**
/**
val => val > 20,
Left("not bigger than 20")
val => val > 20,
Left("not bigger than 20")
val => val > 20,
Left("not bigger than 20")
*/ filterOrElse( filterFn: (either: A) => boolean, otherEither: Left ): Either | Left
/**
/**
/**
/**
/*
*/ toString(): string
/**
/**
return ~~~~~~~~~~~ " + either.toStr() + " ~~~~~~~~~~";
*/ log(): void
/**
return "!!!!!!! " + either.toStr() + " !!!!!!!";
.map(val => val + 5)
.logAndContinue() // => "Right(8)"
.map(val => val + 2)
.filter(val => val > 10)
.logAndContinue(customLogger) // => "!!!!!!! Left() !!!!!!!"
.getOrElse(-1);
/**
*/ static of(val: T, type: 'left' | 'right'): Left | Right
FAQs
A typed, standalone Either object for JavaScript and TypeScript
We found that ethier-sb demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.