Giter Club home page Giter Club logo

future-either's Introduction

future-either

Overview

FutureEither is an extension of the original Future<L, R> data type from fluture. It intends to control the flow of a tri-state object that contains E, L, R branches. The definition of those branches are stated as follows:

  E -> Fatal-Error, should be thrown immediately

  L -> Left-Value, usually for an error but could do side-effects

  R -> Right-Value, returns when succeeded

This requirement comes from real coding practice. Take "querying an user from database" as an example. The process may encounter 3 different situations:

  • query succeed and data is fetched.
  • query failed because of no such user in database.
  • database connection error.

For 1# and 2#, we usually expect further operation (side-effect) may continue execution according to the results;

For 3#, error should throw immediately.

API

fromFuture

FutureEither.fromFuture  :: Future<L, R> -> FutureEither<E, L, R> 

Construct a FutureEither object from existing Future instance.

fromPromise

FutureEither.fromPromise  :: (A -> Promise<R>) -> (A -> FutureEither<E, L, R>)

Construct a function that returns a FutureEither object from a promise function.

Notice: there will be only L, R branch of the FutureEither object, corresponding to Promise.reject and Promise.resolve respectively. E branch will not happen there.

fromP2

FutureEither.fromP2  :: (A -> B -> Promise<R>) -> (A -> B -> FutureEither<E, L, R>)

Similar with fromPromise, but with 2 parameters.

fromP3

FutureEither.fromP3  :: (A -> B -> C -> Promise<R>) -> (A -> B -> C -> FutureEither<E, L, R>)

Similar with fromPromise, but with 3 parameters.

chainRight

FutureEither.prototype.chainRight  :: FutureEither<E, L, R> ~> (R -> Future<E, V>)   -> FutureEither<E, L, V> 

Sequence a new FutureEither object of the R value. It only applies to the R branch, that is, L, E branch will be ignored.

chainLeft

FutureEither.prototype.chainLeft  :: FutureEither<E, L, R> ~> (L -> Future<E, V>)   -> FutureEither<E, V, R>

Sequence a new FutureEither object of the L value. Similar to chainRight(), it only applies to the L branch, that is, R, E branch will be ignored.

mapRight

FutureEither.prototype.mapRight  :: FutureEither<E, L, R> ~> (R -> V)   -> FutureEither<E, L, V>

mapLeft

FutureEither.prototype.mapLeft  :: FutureEither<E, L, R> ~> (L -> V)   -> FutureEither<E, V, R>

chain

FutureEither.prototype.mapLeft  :: FutureEither<E, L, R> ~> (Future<L, R> -> FutureEither<E, L, R>)   -> FutureEither<E, L, R>

chainRej

FutureEither.prototype.mapLeft  :: FutureEither<E, L, R> ~> (E -> FutureEither<E, L, R>)   -> FutureEither<E, L, R>

toValue

FutureEither.prototype.toValue  :: FutureEither<E, L, R> ~> Future<L, R>

Export the future instance with L or R branch for further operations.

toPromiseValue

FutureEither.prototype.toPromiseValue :: FutureEither<E, L, R> ~> Promise<R>

Export the promise object of the future instance. Notice: both E and L branch will throw error!

future-either's People

Contributors

demohn avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

chauchakching

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.