Skip to content

Instantly share code, notes, and snippets.

@tlrobinson
Created November 2, 2012 01:56
Show Gist options
  • Save tlrobinson/3998186 to your computer and use it in GitHub Desktop.
Save tlrobinson/3998186 to your computer and use it in GitHub Desktop.
A REPL that waits for (Promises/A) promises to be resolved before printing.
tlrobinson ~/tmp $ node promise-repl.js
q> require("q-fs").read("promise-repl.js")
<Buffer 0a 76 61 72 20 56 4d 20 3d 20 72 65 71 75 69 72 65 28 22 76 6d 22 29 3b 0a 76 61 72 20 52 45 50 4c 20 3d 20 72 65 71 75 69 72 65 28 22 72 65 70 6c 22 29 ...>
q>
var VM = require("vm");
var REPL = require("repl");
var Q = require("q")
REPL.start({
prompt: "q> ",
eval: function (code, context, file, cb) {
var err, result;
try {
if (this.useGlobal) {
result = VM.runInThisContext(code, file);
} else {
result = VM.runInContext(code, context, file);
}
} catch (e) {
err = e;
}
Q.when(result, function(result) {
cb(null, result);
}, function(error) {
cb(error);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment