regular = 'a string';
enriched = enrichString(regular);
sys.puts(enriched);
function enrichString(str){
//run str through some regex stuff or other string manipulations
return str;
}
Right now it seems to be doing what I hoped it would do but I don’t know if its safe. Could this result in undefined sometimes? Do I need to do something like:
regular = 'a string';
enriched = enrichString(regular, function(data){sys.puts(data);});
function enrichString(str, cb){
//run str through some regex stuff or other string manipulations
cb(str);
}
Thanks for the help!
You only need callbacks if your making asychronous non-blocking calls.
Is safe if
enrichis blocking. For exampleis blocking so it’s safe to do this. Where as
Uses non blocking IO and is not safe. What you want to do is this :
Notice that
enriched = enrichString(regular, sys.puts(data));Does not work because your passing in the return value of
sys.puts(data)as your function parameter (data is undefined aswell!)You need to pass in a function.