i'm using anonymous function perform work on html using restler's get
function:
var some_function() { var outer_var; rest.get(url).on('complete', function(result, response) { if (result instanceof error) { sys.puts('error: ' + result.message); } else { var inner_var; // stuff on **result** build **inner_var** outer_var = inner_var; } }); return outer_var; }
how can value of inner_var
out some_function
scope , return it? have written here doesn't work.
the call asynchronous, take time , call callback later. however, after calling get, script keeps executing , goes next instruction. here happens:
- you call get
- you return outer_var (which still undefined) ... later ...
- get result has arrived , callback called.
- outer_var set
you can't have some_function return value asynchronous, have use callback instead , let code call once data processed.
var some_function(callback) { rest.get(url).on('complete', function(result, response) { if (result instanceof error) { sys.puts('error: ' + result.message); } else { var inner_var; // stuff on **result** build **inner_var** callback(inner_var); } }); }
Comments
Post a Comment