javascript - How to assign value in the callback function? -


i having issues callback function

project.prototype.getfolder = function(path){      this.imagepath;     var instance = this;      function getimage(image, callback) {        var = function(i){          codes.....          //i sure variable image not null                   callback(image);           codes....         }     }     // function image    getimage(image, function (img) {         instance.imagepath = img;    });     //it outputs undefined...    console.log(this.imagepath )  } 

i want assign value this.imagepath inside callback function, seems getting undefined in case. sure pass valid image variable still getting nothing. can provide tip? lot!

your code may asynchronous, hence takes time callback function run. in time, while return value still not set, trying print out variable.

basically though code written after callback, may run before it.

this problem, should try access value after callback been called:

   // function image    getimage(image, function (img) {         instance.imagepath = img;         console.log(instance.imagepath);    }); 

edit:

in order asynchronous parameter return value getfolder, should pass callback function getfolder;

example:

project.prototype.getfolder = function(path, callback){     ...     ...     if (typeof(callback) == "function"){         callback(this.imagepath);     } } 

usage:

project.getfolder(path,function(imagepath){     console.log(imagepath); }); 

Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -