Created
May 15, 2014 05:14
-
-
Save jkrems/3d1e6383e2875820b1f2 to your computer and use it in GitHub Desktop.
Promise aware node.js repl
This file contains 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
var vm = require('vm'); | |
function fancyPromiseEval(code, context, file, cb) { | |
var err, result, script; | |
// first, create the Script object to check the syntax | |
try { | |
script = vm.createScript(code, { | |
filename: file, | |
displayErrors: false | |
}); | |
} catch (e) { | |
return cb(e); | |
} | |
if (!err) { | |
try { | |
result = script.runInContext(context, { displayErrors: false }); | |
} catch (e) { | |
return cb(e); | |
} | |
} | |
if (result && typeof result.nodeify === 'function') { | |
result.nodeify(cb); | |
} else { | |
cb(null, result); | |
} | |
} | |
var repl = require('repl').start({ | |
eval: fancyPromiseEval | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment