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

request-fluture

v2.0.0

Published

Simple HTTP requests with Fluture and request.

Downloads

930

Readme

request-fluture

Build Status

Simple HTTP requests with Flutures and request.

This is a wrapper around request to offer a Fluture API (instead of callback- or promise-based).

Install

# If you are using npm
npm install request-fluture request fluture

# If you are using yarn
yarn add request-fluture request fluture

Usage

Call the exported function with either a url or an options object according to the request docs. It returns a Fluture for your pending request. You can use the whole Fluture API to do stuff with your result.

const request = require('request-fluture');

request('http://example.com')
    .fork(
       error => console.error('Oh no!', error),
       response => console.log('Got a response!', response)
     );

Fetch data from a REST API and extract some specific data.

const request = require('request-fluture');
const { encase } = require('fluture');

request({url: 'https://api.github.com/users/github', headers: {'User-Agent': 'request-fluture'}})
    .map(res => res.body)
    .chain(encase(JSON.parse))
    .map(user => user.name)
    .fork(
      console.error,
      name => console.log(`The requested username is ${name}.`)
    );

You can cleanly cancel the request fluture anytime after using a consuming function like fork on it:

const request = require('request-fluture');

const cancel = request('http://example.com')
    .fork(console.error, console.log);

// Cancel the request
setTimeout(cancel, 1000);

This is for example also used to cleanly cancel requests whose results are not interesting anymore like when using race, saving your precious bandwidth.

Examples

Race

Race multiple requests against each other and resolve to the first settled request.

const Future = require('fluture');
const request = require('request-fluture');

// Race two requests against each other…
request('http://example.com/foo')
  .race(request('http://example.com/bar'))
  .fork(console.error, console.log);

// …or race an array of requests
const first = futures => futures.reduce(Future.race, Future.never);
first([
  request('http://example.com/foo'),
  request('http://example.com/bar'),
  request('http://example.com/baz')
])
  .fork(console.error, console.log);

You can easily implement a timeout for your requests with this:

const Future = require('fluture');
const request = require('request-fluture');

request('http://example.com/foo')
  .race(Future.rejectAfter(1000, 'Timeout'))
  .fork(console.error, console.log);

Parallel requests

Execute five requests with maximum 5 requests in parallel.

const Future = require('fluture');
const request = require('request-fluture');

const tenRequests = Array.from(Array(10).keys())
  .map(resource => request(`http://example.com/${resource}`));

Future.parallel(5, tenRequests)
  .fork(
    console.error,
    results => { results.forEach(console.log); }
  );

Prior art

This is just a slight extension of a Gist by @Avaq.