request-fluture
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 = ; ;
Fetch data from a REST API and extract some specific data.
const request = ;const encase = ; ;
You can cleanly cancel
the request fluture anytime after using a consuming function like fork
on it:
const request = ; const cancel = ; // Cancel the request;
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 = ;const request = ; // Race two requests against each other… ; // …or race an array of requestsconst first = futures; ;
You can easily implement a timeout for your requests with this:
const Future = ;const request = ; ;
Parallel requests
Execute five requests with maximum 5
requests in parallel.
const Future = ;const request = ; const tenRequests = Array ; Future ;
Prior art
This is just a slight extension of a Gist by @Avaq.