javascript - How do you make function with any number of arguments with a callback -
for example have
function passconcatenatedstringback(){ var i,concatstring; for(i=0; i<arguments.length;i++){ concatstring = arguments[i]; } callback(concatstring); // error since callback undefined }
how implement in node callback style? without using promises
function passconcatenatedstringback(){ var i,concatstring, error; var args = array.prototype.slice.call(arguments); //convert array kenichi shibata pointed out var callback = args.pop(); //get callback function , remove arguments. for(i=0; i< args.length;i++){ concatstring += args[i]; } callback(error, concatstring); }
just take last argument , use callback. should still implement validations checking if user provided valid callback.
Comments
Post a Comment