Skip to content

Instantly share code, notes, and snippets.

@jkrems
Created May 15, 2014 05:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkrems/3d1e6383e2875820b1f2 to your computer and use it in GitHub Desktop.
Save jkrems/3d1e6383e2875820b1f2 to your computer and use it in GitHub Desktop.
Promise aware node.js repl
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