Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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
Something went wrong with that request. Please try again.