Skip to content

Commit 87d1b6c

Browse files
committed
add creed asyn ex
1 parent e187c17 commit 87d1b6c

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

examples/13-creed-async.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// https://github.com/briancavalier/creed
2+
// runNode converts Node API with errback into creed promise
3+
const { runNode } = require('creed')
4+
5+
// standard Nodejs library for file access
6+
const fs = require('fs')
7+
8+
// readFilePromise :: String -> String -> Promise Error Buffer
9+
const readFilePromise = (encoding, fileName) => runNode(fs.readFile, fileName, {encoding})
10+
11+
// writeFilePromise :: String -> String -> Promise Error Buffer
12+
const writeFilePromise = fileName => contents => runNode(fs.writeFile, fileName, contents)
13+
14+
15+
readFilePromise('utf-8', 'config.json')
16+
.map( contents => contents.replace(/8/g, '6') )
17+
.chain( writeFilePromise('config-changed.json') )
18+
.map( _ => console.log("Written to 'config-changed.json'!") )
19+
20+
// config.json
21+
// {
22+
// "a": "b",
23+
// "port": 8080
24+
// }
25+
26+
// config-changed.json
27+
// {
28+
// "a": "b",
29+
// "port": 6060
30+
// }

examples/13-task-async.js

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

5-
// for comparison use
65
// https://github.com/Avaq/Fluture
76
const Fluture = require('fluture')
87

@@ -27,7 +26,6 @@ const readFileTask = (fileName, encoding) =>
2726
const readFileFluture = (fileName, encoding) =>
2827
Fluture.node(done => fs.readFile(fileName, encoding, done))
2928

30-
3129
// general purpose Task for writing file
3230
const writeFileTask = (fileName, contents) =>
3331
new Task((rej, res) =>

examples/config-changed.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": 6060
4+
}

0 commit comments

Comments
 (0)