Last active
December 16, 2015 18:47
-
-
Save ajcrites/2f934fabb9ef9a0a4059 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
/* 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(); | |
} |
This file contains hidden or 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
/* 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)); | |
This file contains hidden or 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
/* 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()); |
This file contains hidden or 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
/* 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()); |
This file contains hidden or 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
{ | |
"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