Skip to content

Instantly share code, notes, and snippets.

@nnance
Last active May 5, 2016 17:59
Show Gist options
  • Save nnance/51fdbfb2086f8c3de243c8d9c852c772 to your computer and use it in GitHub Desktop.
Save nnance/51fdbfb2086f8c3de243c8d9c852c772 to your computer and use it in GitHub Desktop.
serialize highland streams
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