JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 72
  • Score
    100M100P100Q77155F
  • License GPL-3.0-only

Wrapper around promise for functional programming

Package Exports

  • @vladbasin/ts-result

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@vladbasin/ts-result) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ts-result

Wrapper around promise for functional programming

Install

npm install @vladbasin/ts-result --save

Getting Started

Let's assume you have loadDataFromBackendAsync() method, which loads data from backend and returns Promise. Normally you use try\catch, async\await, then\catch, if\then\else to handle results. With this library you can write nice readable chains of methods instead:

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

const result = Result
    .FromPromise(loadDataFromBackendAsync())
    .onSuccess(data => console.log(data))
    .ensure(data => data.money > 10, "Not enough money")
    .onFailure(error => alert(error))
    .onSuccess(data => purchase(data.item))
    .delay(3000)
    .onBoth(result => hideLoader())
    //and so on...
    //.onFailure(error => );
    //.onSuccess(() => redirectToPage("/home")))
    .run();

See inline comments for more documentation