When experimenting in the node REPL, I often find myself running
functions that report their result via a callback. When I do so, I get
a REPL prompt immediately back, of course, and this messes up the
output from the function I just called. So I decided I wanted a
facility for making the REPL wait for the callback to be called before
moving on. I came up with the following code:
var geval=eval; // global eval ← javascript magick
function Async (fn) {this.fn=fn;}
global.async=function async (fn) {return new Async(fn);};
function aeval(cmd,context,filename,callback) {
var res;
try {res=geval(cmd);}
catch (e) {callback(e,null)};
if (res instanceof Async) {
res.fn(callback);
} else {
callback(null,res);
}}
require("repl")
.start({prompt:"> ",useGlobal:true,eval:aeval})
.on("exit",process.exit);
Now if I stick this in a file "arepl.js" and run "node arepl.js", I
get a REPL that behaves just like the usual one, unless I wrap a
function in async:
> async(function(cb){setTimeout(function(){cb(null,"hi!")},2000)})
# two seconds pass with no output
'hi!'
>
My approach may be simple minded, but it satisfies my needs.
If someone can suggest a better way, I'm all ears.
Would it be an idea to add a similar mechanism to the standard REPL?
I am sure I am not the only one who could use something like this.
– Harald
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.