GithubHelp home page GithubHelp logo

cancelable-result's Introduction

cancelable-result

A tiny utility for cancelable results

The problem

When we write application logic we want to be very explicit about dealing with errors. We want to have a typed interface for application specific errors, which are dealt with differently than critical language errors like SyntaxError etc. Also writing asynchronous code we want to cancel promises. This is evident with React useEffect where you run promises and want to avoid async dispatches when the effect has been disposed. Currently the language has no built in way of properly dealing with this.

The inspiration

Rust is a language that deals with errors through a Result. There are already implementations of this in JavaScript/TypeScript, like nothrow and ts-results. These are great solutions, but goes way beyond the simple need of returning an async result.

Cancelable promises is very tricky to implement, as you can chain promises. But from the perspective of a cancelable result it becomes more straight forward. By using a typical React pattern of:

React.useEffect(() => {
  let shouldRun = true
  somePromise.then(() => {
    if (shouldRun) {
      // Actually run lgoic
    }
  })
  return () => {
    shouldRun = false
  }
}, [])

we have what we need to create a cancelable result.

The solution

import { Result, Ok, Err } from 'cancelable-result'

const someSideEffect = () => {
  const promise = doSomething()
    .then((value) => Ok(value))
    .catch(() => Err('SOME_ERROR'))
  
  return Result(promise)
}

React.useEffect(() => {
  const { promise, cancel } = someSideEffect()
  
  promise.then((result) => {
    if (result.ok) {
      result.value // Typed result
      
      return
    }
    
    switch (result.error) {
      case 'SOME_ERROR': {

      }
    }
  })
  
  return () => cancel()
}, [])

cancelable-result's People

Contributors

christianalfoni avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.