Last active
December 15, 2015 15:09
-
-
Save wycats/5279394 to your computer and use it in GitHub Desktop.
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
// This is just an example of nested callbacks. I know you could achieve this | |
// goal with simpler code but I'm just showing an example | |
// The well-known callback pattern | |
fs.mkdir("some-path", function(err) { | |
fs.open("some-path/file", "w", function(err, fd) { | |
var buffer = new Buffer("hello world"); | |
fs.write(fd, buffer, 0, buffer.length, null, function(err, bytes) { | |
console.log("Wrote " + bytes + " bytes"); | |
}); | |
}); | |
}); | |
// Nothing is stopping you from doing the same thing with promises. | |
// Slightly different syntax, but the same pattern. Nothing major | |
// new to learn here. | |
fs.mkdir("some-path").then(function() { | |
fs.open("some-path/file", "w").then(function(fd) { | |
var buffer = new Buffer("hello world"); | |
fs.write(fd, buffer, 0, buffer.length, null).then(function(bytes) { | |
console.log("Wrote " + bytes + " bytes"); | |
}); | |
}); | |
}); | |
// With promises, you could also use the chaining features, if you want to | |
fs.mkdir("some-path").then(function() { | |
return fs.open("some-path/file", "w"); | |
}).then(function(fd) { | |
var buffer = new Buffer("hello world"); | |
return fs.write(fd, buffer, 0, buffer.length, null); | |
}).then(function(bytes) { | |
console.log("Wrote " + bytes + " bytes"); | |
}); | |
// In the previous examples, I also ignored error handling. You could still | |
// handle errors for each operation using promises, but you can also have a | |
// catch-all handler for errors that happened at any point during the chain. | |
fs.mkdir("some-path").then(function() { | |
return fs.open("some-path/file", "w"); | |
}).then(function(fd) { | |
var buffer = new Buffer("hello world"); | |
return fs.write(fd, buffer, 0, buffer.length, null); | |
}).then(function(bytes) { | |
console.log("Wrote " + bytes + " bytes"); | |
}).then(null, function(error) { | |
// handle error | |
}); | |
// Remember, these are simply improvements that come along with promises. | |
// If you wanted to treat promises as a slightly different syntax for | |
// the callback-driven approach, that would work just fine. |
No one wants to write any of this. I would much rather write:
fs.mkdir;
fs.open;
fs.write
#!/bin/bash
mkdir some-path
echo hello world > some-path/file
echo "Wrote $(wc -c some-path/file) bytes"
Just shell out to bash for this stuff when you need it. Much more sane.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What I don't understand here is that there's still a
function () {}
callback, it's just that you've got to call another function,.then()
to pass it in instead of just passing it directly which seems much more direct. Then later on, chaining just makes it harder to pass parameters as you need them since the promise library is making the function calls on your behalf.