javascript - callback returns value but apears to be undefined -
i new node , callbacks. trying split list of groups array. groups seperated comma's or "-". last 1 means interval if there 22-25 means: 22, 23, 24, 25. tried following code.
var groups = ["13-16","21-24","33"]; var splitgroups = function(groups, callback){ var result = []; console.log(groups.length); for(var i=0; i<groups.length; i++){ var cbr = callback(groups[i]); result = result.concat(cbr); } return result; } var handlegroupinterval = function(group){ var temparr = []; try { temparr = group.split('-'); } catch(err) { console.log(err.message); } console.log("groups split: "+group); if(temparr.length>1){ var lowerbound = parseint(temparr[0]); var upperbound = parseint(temparr[1]); temparr = []; for(var j=lowerbound; j<=upperbound; j++){ temparr.push(j); } } return temparr; } var tempgroups = splitgroups(groups, function(res){handlegroupinterval(res)}); console.log("end: " +tempgroups);
when set breakpoint on "return temparr" proper values. in first function result thee times undefined. knows problem?
you not return
callback.
simply replace
var tempgroups = splitgroups(groups, function(res){handlegroupinterval(res)});
with
var tempgroups = splitgroups(groups, function(res){ return handlegroupinterval(res); });
notice added return
in callback.
also, think further simplify as
var tempgroups = splitgroups(groups, handlegroupinterval);
Comments
Post a Comment