Skip to content

Instantly share code, notes, and snippets.

@dj-amadeous
Created June 3, 2018 21:24
Show Gist options
  • Save dj-amadeous/d42fa5cde508e1e3f20f9e00988eaacd to your computer and use it in GitHub Desktop.
Save dj-amadeous/d42fa5cde508e1e3f20f9e00988eaacd to your computer and use it in GitHub Desktop.
an example of callback hell
var fs = require('fs');
function append_some_a_to_b(callback) {
fs.open(__dirname + '/a.txt', 'r', function(err, aFd) {
if (err) {
return callback(err);
}
var buffer = new Buffer(10);
fs.read(aFd, buffer, 0, buffer.length, 0, function(err) {
if (err) {
return callback(err);
}
fs.close(aFd, function(err) {
if (err) {
return callback(err);
}
fs.open(__dirname + '/b.txt', 'a', function(err, bFd) {
if (err) {
return callback(err);
}
fs.fstat(bFd, function(err, bStats) {
if (err) {
return callback(err);
}
fs.write(bFd, buffer, 0, buffer.length, bStats.size,
function(err) {
if (err) {
return callback(err);
}
fs.close(bFd, callback);
});
});
});
});
});
});
}
console.log('starting...');
append_some_a_to_b(function(err) {
if (err) {
throw err;
}
console.log('done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment