Git Product home page Git Product logo

io's Introduction

๐Ÿ‰ IO

A small library that makes promises less eager ๐Ÿฅ”

IO<A> is a lightweight wrapper around () => Promise<A> with some useful combinators.

๐Ÿ”ง Installation

npm install @navidjalali/io

โšก๏ธ How to make an IO

You can make a new IO using one of the following ways:

๐Ÿ‹๐Ÿปโ€โ™‚๏ธ Lifting a pure value into an IO:

You can make a successful or failed lazy promise out of a pure value.

IO.succeed(value)
IO.fail(error)
IO.fromNull(maybeNull)
IO.fromUndefined(maybeUndefined)
๐Ÿ‘ท๐Ÿปโ€โ™‚๏ธ Using the constructor:

This is very similar to creating a normal Promise.

new IO<A>((resolve, reject) => {
    // ...
})
๐Ÿฆ† From a thunk that returns a promise

Any promise can be wrapped into an IO.

IO.fromThunk(() => fetch('/api/v1/posts'))
๐Ÿฉ From a function

Exceptions thrown by the function will be lifted into failures.

const unsafeFunction = () => {
    // Unsafe code
}

IO.fromThunkSync(unsafeFunction))

๐Ÿค” How to evaluate an IO?

Simply call .run(). This will create a normal Promise.

โ“ Polling

Any IO that will return some Union type A | InProgress can be polled using IO.poll. This will rerun the IO until it succeeds with a result of type A.

โฐ Scheduling

You can schedule a lazy promise to run later using the scheduleOnce and scheduleForever combinators. You can also make more complicated scheduling logic yourself using IO.sleep and recursion.

๐Ÿ” Retries

You can retry an IO using a RetryPolicy. Currently you can only pick between Spaced and ExponentialWithBackoff

IO.fromThunk(() => fetch('/api/v1/posts'))
    .retry(RetryPolicies.spaced(100, 3))

โณ Timeout

You can effectively get a rejection if your effect runs longer than a specified period using the timeout combinator but please keep in mind that this WILL NOT cancel the Promise created from running this effect or interrupt its finalizers. Hopefully I will make this behaviour better soon.

๐ŸŒˆ More!

There's more you can do. Feel free to check the code, and contribute if you find a new usecase that can be covered!

io's People

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.