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!
-
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 ofgetImage
. – 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 yourgetImage
callback. Problem solved? If you want to get data out ofgetFolder
, 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
2 Answers
Reset to default 5Your 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;
?