javascript - How can I bind data to an event in node.js -
here boiled down version of sort of code i've written:
var cb_gen, item, list, label, token; cb_gen = function (token_string) { return function (data) { var msg = "logout of [" + token_string + "] "; msg += (typeof(data.error) == "undefined") ? "succeeded" : "failed"; console.log(msg); }; }; (item in list) { label = "api_" + item; token = make_token(item); app_resources.event.once(label, cb_gen(token)); }
this has side effect of generating callback function each iteration of loop. i'd rather following:
var cb_gen, item, list, label, token; callback = function (data, token_string) { var msg = "logout of [" + token_string + "] "; msg += (typeof(data.error) == "undefined") ? "succeeded" : "failed"; console.log(msg); }; (item in list) { label = "api_" + item; token = make_token(item); app_resources.event.once(label, callback); app_resources.event.add_bound_data(label, token); }
of course not aware of such method "add_bound_data". first code listed above work , i'm not in dire need of solution second one. however, if loop gets large, second example nice. i'd appreciate insight in know able share.
- thanks
Comments
Post a Comment