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!
-
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;
?
本文标签: javascriptHow to assign value in the callback functionStack Overflow
版权声明:本文标题:javascript - How to assign value in the callback function? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745209087a2647779.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论