Last active
July 2, 2016 01:12
-
-
Save LewisJEllis/1ac2e10e9309b3f59017 to your computer and use it in GitHub Desktop.
Highland reduce example
This file contains 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
var express = require('express'); | |
var _ = require('highland'); | |
var fs = require('fs'); | |
var app = express(); | |
function chain(s, f) { | |
return s.flatMap(_.wrapCallback(f)) | |
} | |
app.post('/process-file', function(req, res) { | |
var inputFile = 'input.txt'; | |
var outputFile = 'output.txt'; | |
var data = _([inputFile]); | |
_([ | |
fs.readFile, | |
process1, | |
process2, | |
process3, | |
writeToFileName(outputFile) | |
]).reduce(data, chain).flatten() | |
.stopOnError(function (err) { | |
return res.status(500).send(err); | |
}).apply(function (data) { | |
return res.status(200).send('processed'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Really helpful!