secret
Last active

SO comment code

  • Download Gist
app.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
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!");
});
output.txt
1 2 3 4 5 6 7 8 9 10
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!

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.