Skip to content

Instantly share code, notes, and snippets.

@ajcrites
Last active December 16, 2015 18:47
Show Gist options
  • Save ajcrites/2f934fabb9ef9a0a4059 to your computer and use it in GitHub Desktop.
Save ajcrites/2f934fabb9ef9a0a4059 to your computer and use it in GitHub Desktop.
/* Continuation Passing
* Written in node pre-ES6 style */
var readline = require("readline");
var userId = "blarg";
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("Please re-enter the userId", confirmUserId);
function confirmUserId(answer) {
if (answer !== userId) {
console.error("Wrong userId provided");
process.exit(1);
}
rl.question("Confirm (y) or (n)", confirmQuestion);
}
function confirmQuestion(answer) {
if ("y" === answer) {
console.log("Completed confirmation");
}
else {
console.log("You did not confirm");
}
rl.close();
}
/* Using promises
* Written in node pre-ES6 style */
var Promise = require("bluebird");
var readline = require("mz/readline");
var userId = "blarg";
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("Please re-enter the userId").then(function (answer) {
if (answer !== userId) {
console.error("Wrong userId provided");
process.exit(1);
}
return rl.question("Confirm (y) or (n)");
}).then(function (answer) {
if ("y" === answer) {
return console.log("Completed confirmation");
}
console.log("You did not confirm");
}).then(rl.close.bind(rl));
/* Using promises
* Written in ES2015 style */
const readline = require("mz/readline");
const userId = "blarg";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("Please re-enter the userId").then(answer => {
if (answer !== userId) {
console.error("Wrong userId provided");
process.exit(1);
}
return rl.question("Confirm (y) or (n)");
}).then(answer => {
if ("y" === answer) {
return console.log("Completed confirmation");
}
console.log("You did not confirm");
}).then(() => rl.close());
/* Using co
* Written in ES2015 style */
const readline = require("mz/readline");
const co = require("co");
const userId = "blarg";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
co(function* () {
let answer = yield rl.question("Please re-enter the userId");
if (answer !== userId) {
console.error("Wrong userId provided");
process.exit(1);
}
answer = yield rl.question("Confirm (y) or (n)");
if ("y" === answer) {
return console.log("Completed confirmation");
}
console.log("You did not confirm");
}).then(() => rl.close());
{
"name": "foo",
"version": "1.0.0",
"description": "",
"main": "i.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"bluebird": "^3.0.6",
"co": "^4.6.0",
"mz": "^2.1.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment