Skip to content

Instantly share code, notes, and snippets.

@skrat
Created January 28, 2015 13:32
Show Gist options
  • Save skrat/d2bfd58201eeed13a377 to your computer and use it in GitHub Desktop.
Save skrat/d2bfd58201eeed13a377 to your computer and use it in GitHub Desktop.
var express = require('express');
var Promise = require("bluebird");
var fs = Promise.promisifyAll(require('fs'));
var app = express();
function pipe() {
return [].reduce.call(arguments, function(red, p) {
return red.then(p);
}
}
app.post('/process-file', function(req, res) {
var inputFile = 'input.txt';
var outputFile = 'output.txt';
pipe( fs.readFileAsync(inputFile),
process1,
process2,
process3,
fs.writeFileAsync.bind(fs, outputFile),
function(data) {
res.status(200).send('processed successfully using bluebird promises');
}
).catch(function(err) {
res.status(500).send(err);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment