Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1850

WIP - @jvlk/rescript-future - Functional Futures for ReScript

$
0
0

I usually stay away from directly interacting with Promises or async/await syntax and use Futures or Tasks instead. In JS/TS land I use fluture or fp-ts for this. There are bindings for libraries that already exist and some older ReScript implementations, but I wanted something lightweight that had an API that was focused on usage in ReScript v11 with the Core library.

It’s not published yet. I’m going to finish the docs and add more examples first.

Here’s an example.

let _ =
  Future.make(() => Fetch.fetch("http://httpstat.us/200"))
  ->Future.flatMap(res => {
    switch res->Fetch.Response.ok {
    | true => Ok(res)
    | false =>
      Error(`${res->Fetch.Response.status->Int.toString}: ${res->Fetch.Response.statusText}`)
    }
  })
  ->Future.map(res => res->Fetch.Response.statusText)
  ->Future.fold(Console.error, Console.log)

A future will never throw an error, even if the promise call does. It gets caught and returned as an Error.


Viewing all articles
Browse latest Browse all 1850

Trending Articles