npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rescript-future

v2.1.0

Published

> Cancellable futures for ReScript

Downloads

887

Readme

Future

Cancellable futures for ReScript

Installation

Run the following in your console:

$ yarn add rescript-future

Then add rescript-future to your bsconfig.json's bs-dependencies:

 {
   "bs-dependencies": [
+    "rescript-future"
   ]
 }

Basics

A Future is a data structure that represents a potential value. It works both synchronously & asynchrously. It consists of 3 possible states:

  • Pending: The value is yet to be resolved
  • Cancelled: The future has been cancelled before could resolve
  • Resolved: The future holds its value
// Basic synchronous future
Future.value(1)
->Future.map(x => x + 1)
->Future.flatMap(x => Future.value(x + 1))
->Future.get(Js.log)
// Logs: 3

API

See the details in the interface file

Create

  • value('a) => Future.t<'a>: creates a resolved future
  • makePure(('a => unit) => unit) => Future.t<'a>: creates a future
  • make(('a => unit) => option<unit => unit>) => Future.t<'a>: creates a future with a cancellation effect

Cancel

  • cancel(future): Cancels a future and its dependents

Extract

  • get(future, cb) => unit: Executes cb with future's resolved value

Transform

  • map(future, mapper): Returns a new mapped future
  • flatMap(future, mapper): Returns a new mapped future with mapper returning a future itself

Test

  • isPending(future) => bool
  • isCancelled(future) => bool
  • isResolved(future) => bool

Result transforms

  • mapResult(future, mapper)
  • mapOk(future, mapper)
  • flatMapOk(future, mapper)
  • mapError(future, mapper)
  • flatMapError(future, mapper)

Debug

  • tap(future, cb) => future
  • tapOk(resultFuture, cb) => resultFuture
  • tapError(resultFuture, cb) => resultFuture

Multiple futures

  • all2((future, future))
  • all3((future, future, future))
  • all4((future, future, future, future))
  • all5((future, future, future, future, future))
  • all6((future, future, future, future, future, future))
  • all(array<future>)

Interop

  • FuturePromise.fromPromise
  • FuturePromise.toPromise
  • FuturePromise.resultToPromise

Cancellation

In JavaScript, Promises are not cancellable. That can be limiting at times, especially when using React's useEffect, that let's you return a cancellation effect in order to prevent unwanted side-effects.

let valueFromServer = Future.make(resolve => {
  let request = getFromServer((err, data) => {
    if err {
      resolve(Error(err))
    } else {
      resolve(Ok(data))
    }
  })
  Some(() => cancelRequest(request))
})

let deserializedValueFromServer = 
  valueFromServer->Future.map(deserialize)

Future.cancel(valueFromServer)
// valueFromServer & deserializedValueFromServer are cancelled if they were still pending

Aknowledgments

Heavily inspired by RationalJS/future's API