Socket
Socket
Sign inDemoInstall

@vladbasin/ts-result

Package Overview
Dependencies
6
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @vladbasin/ts-result

Wrapper around promise for functional programming


Version published
Weekly downloads
151
increased by75.58%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

ts-result

Node.js CI

This library brings elements of functional programming to TypeScript/JavaScript. See Use cases section for details.

Install

npm

npm install @vladbasin/ts-result

yarn

yarn add @vladbasin/ts-result

Use cases

Asynchronous code chaining

Let's assume you have to work with the set of async methods, which load data from backend and return Promise. Normally you use try\catch, async\await, then\catch, if\then\else to handle results. Readability isn't really good with this approach.


showLoader();
try {
    const wallet = await getWalletAsync();
    if (wallet.money < 10) {
        alert("Not enough money");
        return;
    }
    const item = await getItemAsync();
    const response = await purchaseAsync(item);
    if (!response.success) {
        alert(response.error);
        return;
    }
    log("Purchase success");
}
catch (error) {
    alert(error);
}
finally {
    hideLoader();
}

However, with this library instead you can write nice readable chains of methods:

import { Result } from "@vladbasin/Result";

showLoader();
Result
    .FromPromise(getWalletAsync())
    .ensure(wallet => wallet.money > 10, "Not enough money")
    .onSuccess(() => getItemAsync(itemId))
    .onSuccess(item => purchaseAsync(item))
    .ensure(response => response.success, response.error)
    .onFailure(error => alert(error))
    .onSuccess(() => log("Purchase success"))
    .onBoth(result => hideLoader())
    .run();

Rid of primitive obsession

Without this library (poor readability, code repeats)

const usernameValidation = validateUsername(username);
if (!usernameValidation.success) {
    alert(usernameValidation.error)
    return;
}
const passwordValidation = validatePassword(password);
if (!passwordValidation.success) {
    alert(passwordValidation.error)
    return;
}
const passwordRepeatValidation = validatePasswordRepeat(passwordRepeat, password);
if (!passwordRepeatValidation.success) {
    alert(passwordRepeatValidation.error)
    return;
}

With this library (readable code, reusable logic)

import { Result } from "@vladbasin/Result";

Result
    .Start()
    .onSuccess(() => validateUsername(username))
    .onSuccess(() => validatePassword(password))
    .onSuccess(() => validatePasswordRepeat(passwordRepeat, password))
    .onFailure(error => alert(error))
    .run();

Other handy API

This library also provides API to retry(), delay(), and combine() asynchonous code. See inline comments for more documentation

Keywords

FAQs

Last updated on 13 Jan 2024

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc