Skip to content

Commit 692ff15

Browse files
committed
Add fluture example
1 parent fb543d6 commit 692ff15

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

examples/13-task-async.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// part of Folktale https://github.com/origamitower/folktale
33
const Task = require('data.task')
44

5+
// for comparison use
6+
// https://github.com/Avaq/Fluture
7+
const Fluture = require('fluture')
8+
59
// standard Nodejs library for file access
610
const fs = require('fs')
711

@@ -17,6 +21,10 @@ const readFile = (fileName, encoding) =>
1721
err ? rej(err) : res(contents)
1822
))
1923

24+
const readFileFl = (fileName, encoding) =>
25+
Fluture.node(done => fs.readFile(fileName, encoding, done))
26+
27+
2028
// general purpose Task for writing file
2129
const writeFile = (fileName, contents) =>
2230
new Task((rej, res) =>
@@ -25,7 +33,11 @@ const writeFile = (fileName, contents) =>
2533
))
2634

2735

28-
const app = _ =>
36+
const writeFileFl = (fileName, contents) =>
37+
Fluture.node(done => fs.writeFile(fileName, contents, done))
38+
39+
40+
const app =
2941

3042
// read file - returns Task
3143
readFile('config.json', 'utf-8')
@@ -39,9 +51,19 @@ const app = _ =>
3951

4052

4153
// only now launch the task
42-
app().fork( showErr, showSuc )
54+
app.fork( showErr, _ => showSuc("Check 'config1.json'!") )
55+
56+
57+
58+
// optionally using Fluture
59+
// define the application
60+
const appFl = readFileFl('config.json', 'utf-8')
61+
.map( contents => contents.replace(/8/g, '7') )
62+
.chain( contents => writeFileFl('config1-fl.json', contents))
63+
64+
// now call it
65+
appFl.fork( showErr, _ => showSuc("Check 'config1-fl.json'!"))
4366

44-
console.log("Check 'config1.json'!")
4567

4668

4769
// optionally using Futurize
@@ -55,13 +77,11 @@ const readFuture = future(fs.readFile)
5577
const writeFuture = future(fs.writeFile)
5678

5779
// setup the task
58-
const app1 = _ =>
59-
readFuture('config.json', 'utf-8')
80+
const app1 = readFuture('config.json', 'utf-8')
6081
.map( contents => contents.replace(/8/g, '6') )
6182
.chain( contents => writeFuture('config2.json', contents) )
6283

6384
// run the task
64-
app1().fork( showErr, showSuc )
85+
app1.fork( showErr, _ => showSuc("Check 'config2.json'!") )
6586

66-
console.log("Check 'config2.json'!")
6787

examples/config1-fl.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"a": "b",
3+
"port": 7070
4+
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"data.either": "^1.4.0",
2828
"data.task": "^3.1.1",
2929
"fantasy-identities": "0.0.1",
30+
"fluture": "^4.3.0",
3031
"futurize": "^1.0.2",
3132
"immutable": "^3.8.1",
3233
"immutable-ext": "^1.0.6",

yarn.lock

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ [email protected]:
124124
dependencies:
125125
daggy "0.0.1"
126126

127+
fluture@^4.3.0:
128+
version "4.3.0"
129+
resolved "https://registry.yarnpkg.com/fluture/-/fluture-4.3.0.tgz#cc08cce21d2497093cffb0aec7e390af53168742"
130+
dependencies:
131+
inspect-f "^1.2.0"
132+
sanctuary-type-classes "^2.0.1"
133+
127134
forever-agent@~0.6.1:
128135
version "0.6.1"
129136
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
@@ -204,6 +211,10 @@ immutable@^3.8.1:
204211
version "3.8.1"
205212
resolved "https://registry.yarnpkg.com/immutable/-/immutable-3.8.1.tgz#200807f11ab0f72710ea485542de088075f68cd2"
206213

214+
inspect-f@^1.2.0:
215+
version "1.2.0"
216+
resolved "https://registry.yarnpkg.com/inspect-f/-/inspect-f-1.2.0.tgz#80b8f1995e36222d11f22ed66d9b7d8559c400c9"
217+
207218
is-my-json-valid@^2.12.4:
208219
version "2.15.0"
209220
resolved "https://registry.yarnpkg.com/is-my-json-valid/-/is-my-json-valid-2.15.0.tgz#936edda3ca3c211fd98f3b2d3e08da43f7b2915b"
@@ -312,6 +323,16 @@ request@^2.79.0:
312323
tunnel-agent "~0.4.1"
313324
uuid "^3.0.0"
314325

326+
sanctuary-type-classes@^2.0.1:
327+
version "2.0.1"
328+
resolved "https://registry.yarnpkg.com/sanctuary-type-classes/-/sanctuary-type-classes-2.0.1.tgz#06b6cfd78e3e8d85cd93f5c2d7b08796b68d1e74"
329+
dependencies:
330+
sanctuary-type-identifiers "1.0.x"
331+
332+
333+
version "1.0.0"
334+
resolved "https://registry.yarnpkg.com/sanctuary-type-identifiers/-/sanctuary-type-identifiers-1.0.0.tgz#e8f359f006cb5e624cfb8464603fc114608bde9f"
335+
315336
316337
version "1.0.9"
317338
resolved "https://registry.yarnpkg.com/sntp/-/sntp-1.0.9.tgz#6541184cc90aeea6c6e7b35e2659082443c66198"

0 commit comments

Comments
 (0)