Last active
May 5, 2016 17:59
-
-
Save nnance/51fdbfb2086f8c3de243c8d9c852c772 to your computer and use it in GitHub Desktop.
serialize highland streams
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require("fs") | |
const _ = require("highland") | |
const files = [ | |
".editorconfig", | |
".eslintrc" | |
] | |
function usePromises() { | |
const promises = files.map(file => { | |
return new Promise((resolve, reject) => { | |
fs.readFile(file, "utf8", (err, data) => { | |
if (err) { | |
reject() | |
} else { | |
resolve(data) | |
} | |
}) | |
}) | |
}) | |
function resolvePromises(index) { | |
if (index < promises.length) { | |
Promise.resolve(promises[index]).then(data => { | |
console.log(data) | |
resolvePromises(index + 1) | |
}) | |
} | |
} | |
resolvePromises(0) | |
} | |
function useStreams() { | |
const readFile = _.wrapCallback(fs.readFile) | |
const filesStream = _(files) | |
filesStream | |
.map(readFile) | |
.sequence() | |
.map(upperCase) | |
.pipe(process.stdout) | |
function upperCase(data) { | |
return data.toString().toUpperCase() | |
} | |
} | |
useStreams() | |
// usePromises() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment