fetch-future

A fetch that returns Futures instead of Promises

MIT License

Stars
4

Fetch-Future

Use fetch as a Monadic Future

What? Fetch the future?

No just use fetch that returns a Future instead of a promise.

  • Minimal: just fetch() with headers and text/json/xml responses
  • Modern: written in ES2015
  • Cancellable you can cancel the fetch once dispatched
  • Add your own Future you can add your own Future Library as long as it implements the
  • Monadic

🤔 What's Not there?

  • Uses simple Arrays instead of Iterables
  • No streaming, just Futurizes existing XMLHttpRequest response bodies

For more information about Futures see:

Usage

yarn install https://github.com/theodesp/fetch-future.git

add your future flavor

yarn add fluture // or data.task but no ramda-fantasy as it cannot abort!

Example

Fetch some objects from an api

import { fetchF } from 'fetch-future';
import Future from 'fluture';

const fetch = fetchF(Future);

fetch('https://jsonplaceholder.typicode.com/posts')
  .chain(res => res.json()) // json() is also a Future!
  .fork(console.error, console.log) // Future Fantasy!

Cancel a future!

import { fetchF } from 'fetch-future';
import Future from 'fluture';

const fetch = fetchF(Future);

const future = fetch('https://jsonplaceholder.typicode.com/posts')
  .chain(res => res.json()) // json() is also a Future!
  .fork(console.error, console.log) // Future  to be resolved!
  
future.cancel(); // Aborts the fetch by calling cancel!

Fetch Caveats

  • By default, fetch won't send or receive any cookies from the server, resulting in unauthenticated
    requests if the site relies on maintaining a user session.
fetch('/posts', {
  credentials: 'include'
});
  • The Promise returned from fetch() won't reject on HTTP error status
    even if the response is an HTTP 404 or 500.
    Instead, it will resolve normally, and it will only reject
    on network failure or if anything prevented the request from completing.

API

fetchF :: Constructor -> CPS -> ( (input, options) -> Future )

License

MIT © theodesp