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

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -