Giter VIP home page Giter VIP logo

fetch-future's Introduction

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

fetch-future's People

Contributors

theodesp avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

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.