admin管理员组

文章数量:1415134

I am having issues with a callback function

project.prototype.getFolder = function(path){

    this.imagePath;
    var instance = this;

    function getImage(image, callback) {
       var it = function(i){
         codes.....
         //I am sure variable image is not null
                  callback(image);

         codes....
        }
    }

   // function to get the image
   getImage(image, function (img) {
        instance.imagePath = img;
   });

   //it outputs undefined...
   console.log(this.imagePath )

}

I want to assign the value to this.imagePath inside the callback function, but it seems I am getting undefined in my case. I am sure I pass valid image variable but I am still getting nothing. Can anyone provide a tip? Thanks a lot!

I am having issues with a callback function

project.prototype.getFolder = function(path){

    this.imagePath;
    var instance = this;

    function getImage(image, callback) {
       var it = function(i){
         codes.....
         //I am sure variable image is not null
                  callback(image);

         codes....
        }
    }

   // function to get the image
   getImage(image, function (img) {
        instance.imagePath = img;
   });

   //it outputs undefined...
   console.log(this.imagePath )

}

I want to assign the value to this.imagePath inside the callback function, but it seems I am getting undefined in my case. I am sure I pass valid image variable but I am still getting nothing. Can anyone provide a tip? Thanks a lot!

Share Improve this question edited May 2, 2013 at 19:14 FlyingCat asked May 2, 2013 at 19:09 FlyingCatFlyingCat 14.3k36 gold badges128 silver badges201 bronze badges 5
  • 1 Might some of those codes..... include an asynchronous operation? Because asynchronous operations must wait for the current function stack to resolve before they can resolve, console.log is running before whatever asynchronous operation is ordered inside of getImage. – apsillers Commented May 2, 2013 at 19:12
  • 1 console.log(this.validImagePath)... where do you update validImagePath? – Yauhen Vasileusky Commented May 2, 2013 at 19:13
  • sorry guys, it's a typo. and yes apsillers i have an asynchronous function in my codes...see updates. – FlyingCat Commented May 2, 2013 at 19:15
  • So put console.log(this.imagePath) inside your getImage callback. Problem solved? If you want to get data out of getFolder, pass a callback into that as well – apsillers Commented May 2, 2013 at 19:16
  • Is there a call to it() in getImage function()? I don't see it. – Ceres Commented May 2, 2013 at 19:16
Add a ment  | 

2 Answers 2

Reset to default 5

Your code may be asynchronous, hence it takes time for the callback function to run. in this time, while your return value is still not set, you are trying to print out the variable.

Basically even though the code itself is written after the callback, it may run before it.

This is probably your problem, so you should try to access this value only after the callback as been called:

   // function to get the image
   getImage(image, function (img) {
        instance.imagePath = img;
        console.log(instance.imagePath);
   });

EDIT:

In order to get the asynchronous parameter back as a return value for getFolder, you should pass a callback function to getFolder;

Example:

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

Usage:

project.getFolder(path,function(imagePath){
    console.log(imagePath);
});

You assign imagePath, but try to access to validImagePath. try :

 console.log(this.imagePath)

And I don't understand why you have this.imagePath; at the beginning. May be you want to write :

this.imagePath = path;

?

本文标签: javascriptHow to assign value in the callback functionStack Overflow