From 54c4ae06b485256294c549221e3eff40ec30eab2 Mon Sep 17 00:00:00 2001 From: "Ahmed K. A" Date: Wed, 24 May 2023 19:09:52 +0300 Subject: [PATCH] FEAT: Add okOr which picks a default value inplace or an error --- src/new.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/new.ts b/src/new.ts index 679845f..711416e 100644 --- a/src/new.ts +++ b/src/new.ts @@ -4,6 +4,11 @@ export function isError(possibleErr: Result): possibleErr is Error { return possibleErr instanceof Error; } +export function okOr(result: Result, orValue: T) { + if (isError(result)) return orValue; + else return result; +} + export async function tryAsync(promise: Promise): Promise> { try { const ok = await promise;