From 19db154c0742ca658123e2fcfeb6261955f8ad2c Mon Sep 17 00:00:00 2001 From: laggingreflex Date: Tue, 2 Feb 2016 21:03:30 +0530 Subject: [PATCH] implements promise, makes callback optional checks if the last argument provided was a function if so, it calls the original function as is if not, it returns a promise whose `resolve` function is passed as the callback to the original function thus resolving the promise when function returns a value (relies on native Promise implementation) if Promise is undefined, it calls original function as is --- README.md | 17 +++++++++++++++++ index.js | 7 ++++--- promisify.js | 17 +++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 promisify.js diff --git a/README.md b/README.md index 4f6882b..ca9033a 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,12 @@ Usage - `message`: text prompt for the user - `handler`: function to be called with the entered text +or returns a promise if handler not provided: + +#### `promise = prompt(message)` + +- `promise`: promise which resolves with the entered text + Example ------- @@ -33,6 +39,17 @@ prompt('enter your first name: ', function (val) { }); }); ``` +#### with promise: +```js +var first, last; +prompt('enter your first name: ').then(function (val) { + first = val; + return prompt('and your last name: '); +}).then(function (val) { + last = val; + console.log('hi, ' + first + ' ' + last + '!'); +}); +``` ### Password/hidden input diff --git a/index.js b/index.js index 4c14ae7..243b97c 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,6 @@ var tty = require('tty') , keypress = require('keypress') + , promisify = require('./promisify') function prompt (message, hideInput, cb) { if (typeof hideInput === 'function') { @@ -53,7 +54,7 @@ function prompt (message, hideInput, cb) { var line = ''; process.stdin.on('keypress', listen).resume(); } -module.exports = prompt; +module.exports = promisify(prompt); function password (message, cb) { prompt(message, true, function (val) { @@ -62,7 +63,7 @@ function password (message, cb) { else cb(val, function () {}); // for backwards-compatibility, fake end() callback }); } -module.exports.password = password; +module.exports.password = promisify(password); function multi (questions, cb) { var idx = 0, ret = {}; @@ -113,4 +114,4 @@ function multi (questions, cb) { prompt(label, q.type === 'password', record); })(); } -module.exports.multi = multi; +module.exports.multi = promisify(multi); diff --git a/promisify.js b/promisify.js new file mode 100644 index 0000000..dd2a5df --- /dev/null +++ b/promisify.js @@ -0,0 +1,17 @@ +module.exports = function promisify(fn) { + return function() { + if (typeof Promise === 'undefined') + return fn.apply(null, arguments); + + var args = [].slice.call(arguments); + var cb = args[args.length - 1]; + if (typeof cb !== 'function') { + return new Promise(function(resolve) { + args.push(resolve); + fn.apply(null, args); + }); + } else { + return fn.apply(null, args); + } + } +}