Skip to content

Instantly share code, notes, and snippets.

@BinaryMuse
Created August 20, 2012 19:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BinaryMuse/77ce3468303351c06f29 to your computer and use it in GitHub Desktop.
Save BinaryMuse/77ce3468303351c06f29 to your computer and use it in GitHub Desktop.
SO comment code
var fs = require('fs');
var prompt = require('prompt');
// Just a stub to test functionality
fs.fakeExists = function(file, callback) {
callback(true);
}
// Another stub so we're not actually creating files
fs.fakeWriteFile = function(file, data, callback) {
callback();
}
function startProcess(callback) {
prompt.get("file", function(err, results) {
fs.fakeExists(results.file, function(exists) {
if (exists) askOverwrite(results.file, callback);
else createFile(results.file, callback);
});
});
}
function askOverwrite(file, callback) {
console.log("File '" + file + "' exists.");
prompt.get("overwrite", function(err, results) {
if (err) return callback(err);
if (results.overwrite == 'yes') createFile(file, callback);
else abort();
});
}
function createFile(file, callback) {
fs.fakeWriteFile(file, "Some data", callback);
}
function abort() {
console.log("Aborting creation.");
process.exit(1);
}
startProcess(function() {
console.log("All done!");
});
btilley@mbair ~/src/sandbox/nodeasync node waterfall.js
prompt: file: test.txt
File 'test.txt' exists.
prompt: overwrite: no
Aborting creation.
btilley@mbair ~/src/sandbox/nodeasync node waterfall.js prompt: file: test.txt
File 'test.txt' exists.
prompt: overwrite: yes
All done!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment