Public
Script
Readme

Usage

import { Failure, Success } from "https://esm.town/v/pomdtr/neverthrow?v=5"; const demoFunction = () => { const result = Math.random(); if (result > 0.5) { return Failure( "Math. random produced too high a number", ); } return Success(result); }; const res = demoFunction(); if (res.ok) { console.log(res.value); } else { console.error(res.error); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
interface Success<T> {
ok: true;
value: T;
}
interface Failure<E> {
ok: false;
error: E;
}
export function Success<T>(value: T): Success<T> {
return { ok: true, value };
}
export function Failure<E>(error: E): Failure<E> {
return { ok: false, error };
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
August 16, 2024