Sunday, October 6, 2019

javascript: pass return value in callback function



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 ?


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

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...