Created
January 9, 2015 13:57
-
-
Save pmuellr/b0fc9a4a6e4b75b1d06b to your computer and use it in GitHub Desktop.
another way to flatten your JavaScript callback pyramids
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
// another kind of flattening; see: http://blog.vullum.io/javascript-flow-callback-hell-vs-async-vs-highland/ | |
var express = require('express') | |
var fs = require('fs') | |
var app = express() | |
app.post('/process-file', onProcessFile) | |
function onProcessFile(req, res) { | |
var inputFile = 'input.txt' | |
var outputFile = 'output.txt' | |
fs.readFile(inputFile, onReadFile) | |
function onReadFile(err, data) { | |
if (err) return res.status(500).send(err) | |
process1(data, onProcess1) | |
} | |
function onProcess1(err, data) { | |
if (err) return res.status(500).send(err) | |
process2(data, onProcess2) | |
} | |
function onProcess2(err, data) { | |
if (err) return res.status(500).send(err) | |
process3(data, onProcess3) | |
} | |
function onProcess3(err, data) { | |
if (err) return res.status(500).send(err) | |
fs.writeFile(outputFile, data, onWriteFile) | |
} | |
function onWriteFile(err) { | |
if (err) return res.status(500).send(err) | |
res.status(200).send('processed successfully using callback hell') | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment