Tuesday 28 November 2017

javascript: pass return value in callback function

itemprop="text">

Please help me understand
this:




I have an object
self.domainSettings() which has a save
method. The save returns the result which I want to use in
callback function to populate success or error message. I am using following
code:



self.domainSettings().save(function(data){

console.log('in here');
console.log(data);
console.log('outta
here');
}());

function save(){


return
'success';
};


When
I do console.log(data) is it guaranteed that the save has
finished executing ? Also it says data is undefined because the return value of save is
not getting passed to callback function I guess. How do I get that
?


style="font-weight: bold;">

Answer




You have to pass callback in
save() in order to access the result from a
callback.



function
save(fn){
return
fn('success');

};


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...