77//. ```
88//.
99//. ## API
10+ //.
11+ //. Note: All examples assume that the following code is included:
12+ //.
13+ //. ```js
14+ //. > import {value} from 'fluture/index.js'
15+ //. > const show = m => { value (log ('result')) (m) }
16+ //. ```
1017
1118import http from 'http' ;
1219import https from 'https' ;
@@ -35,10 +42,12 @@ import {
3542//. itself from the event emitter.
3643//.
3744//. ```js
38- //. > const emitter = new EventEmitter ();
39- //. > setTimeout (() => emitter.emit ('answer', 42), 100);
40- //. > once ('answer') (emitter);
41- //. Future.of (42);
45+ //. > import {EventEmitter} from 'events'
46+ //. > const emitter = new EventEmitter ()
47+ //. > setTimeout (() => emitter.emit ('answer', 42), 100)
48+ //. > show (once ('answer') (emitter))
49+ //. undefined
50+ //. [result]: 42
4251//. ```
4352export const once = event => emitter => Future ( ( rej , res ) => {
4453 const removeListeners = ( ) => {
@@ -67,8 +76,9 @@ export const once = event => emitter => Future ((rej, res) => {
6776//. with an Error if the encoding is unknown.
6877//.
6978//. ```js
70- //. > encode ('utf8') (Buffer.from ('Hello world!'));
71- //. 'Hello world!'
79+ //. > show (encode ('utf8') (Buffer.from ('Hello world!')))
80+ //. [result]: 'Hello world!'
81+ //. undefined
7282//. ```
7383export const encode = encoding => buffer => (
7484 mapRej ( e => new Error ( e . message ) )
@@ -107,14 +117,15 @@ export const emptyStream = streamOf (Buffer.alloc (0));
107117//. itself from the Stream.
108118//.
109119//. ```js
110- //. > const stream = new Readable ({read: () => {}});
120+ //. > const stream = new Readable ({read: () => {}})
111121//. > setTimeout (() => {
112- //. . stream.push ('hello');
113- //. . stream.push ('world');
114- //. . stream.push (null);
115- //. . }, 100);
116- //. > buffer (stream);
117- //. Future.of ([Buffer.from ('hello'), Buffer.from ('world')]);
122+ //. . stream.push ('hello')
123+ //. . stream.push ('world')
124+ //. . stream.push (null)
125+ //. . }, 100)
126+ //. > show (buffer (stream))
127+ //. undefined
128+ //. [result]: [Buffer.from ('hello'), Buffer.from ('world')]
118129//. ```
119130export const buffer = stream => Future ( ( rej , res ) => {
120131 const chunks = [ ] ;
@@ -158,8 +169,9 @@ export const bufferString = charset => stream => (
158169//. blocking the event loop until it's completed.
159170//.
160171//. ```js
161- //. > instant ('noodles')
162- //. Future.of ('noodles')
172+ //. > show (instant ('noodles'))
173+ //. undefined
174+ //. [result]: 'noodles'
163175//. ```
164176export const instant = x => Future ( ( rej , res ) => {
165177 process . nextTick ( res , x ) ;
@@ -174,8 +186,9 @@ export const instant = x => Future ((rej, res) => {
174186//. job is unscheduled.
175187//.
176188//. ```js
177- //. > immediate ('results')
178- //. Future.of ('results')
189+ //. > show (immediate ('results'))
190+ //. undefined
191+ //. [result]: 'results'
179192//. ```
180193export const immediate = x => Future ( ( rej , res ) => {
181194 const job = setImmediate ( res , x ) ;
@@ -188,23 +201,23 @@ export const immediate = x => Future ((rej, res) => {
188201//. below, in order to cover a wide variety of HTTP-related use cases.
189202//.
190203//. ```js
191- //. import {map, chain, chainRej, encase, fork} from 'fluture/index.js';
204+ //. import {map, chain, chainRej, encase, fork} from 'fluture/index.js'
192205//. import {retrieve,
193206//. acceptStatus,
194207//. autoBufferResponse,
195- //. responseToError } from './index.js';
208+ //. responseToError } from './index.js'
196209//.
197210//. const rejectUnacceptable = res => (
198211//. acceptStatus (200) (res)
199212//. .pipe (chainRej (responseToError))
200- //. );
213+ //. )
201214//.
202215//. retrieve ('https://api.github.com/users/Avaq') ({'User-Agent': 'Avaq'})
203216//. .pipe (chain (rejectUnacceptable))
204217//. .pipe (chain (autoBufferResponse))
205218//. .pipe (chain (encase (JSON.parse)))
206219//. .pipe (map (avaq => avaq.name))
207- //. .pipe (fork (console.error) (console.log));
220+ //. .pipe (fork (console.error) (console.log))
208221//. ```
209222//.
210223//. The example above will either:
@@ -267,17 +280,17 @@ const getRequestModule = protocol => {
267280//. [IncomingMessage][]. If the Future is cancelled, the request is aborted.
268281//.
269282//. ```js
270- //. import {attempt, chain} from 'fluture/index.js';
271- //. import {createReadStream} from 'fs';
283+ //. import {attempt, chain} from 'fluture/index.js'
284+ //. import {createReadStream} from 'fs'
272285//.
273286//. const sendBinary = request ({
274287//. method: 'POST',
275288//. headers: {'Transfer-Encoding': 'chunked'},
276- //. });
289+ //. })
277290//.
278- //. const eventualBody = attempt (() => createReadStream ('./data.bin'));
291+ //. const eventualBody = attempt (() => createReadStream ('./data.bin'))
279292//.
280- //. eventualBody.pipe (chain (sendBinary ('https://example.com')));
293+ //. eventualBody.pipe (chain (sendBinary ('https://example.com')))
281294//. ```
282295//.
283296//. If you want to use this function to transfer a stream of data, don't forget
@@ -346,7 +359,7 @@ export const send = mime => method => url => extraHeaders => buf => {
346359//. sendJson ('PUT')
347360//. ('https://example.com/users/bob')
348361//. ({Authorization: 'Bearer asd123'})
349- //. ({name: 'Bob', email: 'bob@example .com'});
362+ //. ({name: 'Bob', email: 'bob@example .com'})
350363//. ```
351364export const sendJson = method => url => headers => json => {
352365 const buf = Buffer . from ( JSON . stringify ( json ) ) ;
@@ -365,7 +378,7 @@ export const sendJson = method => url => headers => json => {
365378//. sendForm ('POST')
366379//. ('https://example.com/users/create')
367380//. ({})
368- //. ({name: 'Bob', email: 'bob@example .com'});
381+ //. ({name: 'Bob', email: 'bob@example .com'})
369382//. ```
370383export const sendForm = method => url => headers => form => {
371384 const buf = Buffer . from ( qs . stringify ( form ) ) ;
0 commit comments