admin管理员组文章数量:1026989
My code looks like this:
myObject.myMethod('imageCheck', function () {
var image = new Image();
image.onerror = function() {
return false;
};
image.onload = function() {
return true;
};
image.src = '.jpg';
});
But, it doesn't work, assumedly because my returns only return from the anonymous function, not from the one called imageCheck. How can I rewrite this so that the whole function is returned as true or false?
My code looks like this:
myObject.myMethod('imageCheck', function () {
var image = new Image();
image.onerror = function() {
return false;
};
image.onload = function() {
return true;
};
image.src = 'http://www.example./image.jpg';
});
But, it doesn't work, assumedly because my returns only return from the anonymous function, not from the one called imageCheck. How can I rewrite this so that the whole function is returned as true or false?
Share Improve this question asked Apr 27, 2011 at 19:13 Rich BradshawRich Bradshaw 73.1k46 gold badges188 silver badges241 bronze badges 1- What is myMethod and what is imageCheck? Why are you returning something in a handler function for an async call? That return data does not do anything. You have to use the "continuation" style of programming for async. – Stephen Chung Commented Apr 28, 2011 at 9:53
1 Answer
Reset to default 6you have to use callbacks, for example:
myObject.myMethod('imageCheck', function () {
var image = new Image();
image.onerror = function() {
returnCallback(false);
};
image.onload = function() {
returnCallback(true);
};
image.src = 'http://www.example./image.jpg';
});
function returnCallback(bool){
//do something with bool
}
My code looks like this:
myObject.myMethod('imageCheck', function () {
var image = new Image();
image.onerror = function() {
return false;
};
image.onload = function() {
return true;
};
image.src = '.jpg';
});
But, it doesn't work, assumedly because my returns only return from the anonymous function, not from the one called imageCheck. How can I rewrite this so that the whole function is returned as true or false?
My code looks like this:
myObject.myMethod('imageCheck', function () {
var image = new Image();
image.onerror = function() {
return false;
};
image.onload = function() {
return true;
};
image.src = 'http://www.example./image.jpg';
});
But, it doesn't work, assumedly because my returns only return from the anonymous function, not from the one called imageCheck. How can I rewrite this so that the whole function is returned as true or false?
Share Improve this question asked Apr 27, 2011 at 19:13 Rich BradshawRich Bradshaw 73.1k46 gold badges188 silver badges241 bronze badges 1- What is myMethod and what is imageCheck? Why are you returning something in a handler function for an async call? That return data does not do anything. You have to use the "continuation" style of programming for async. – Stephen Chung Commented Apr 28, 2011 at 9:53
1 Answer
Reset to default 6you have to use callbacks, for example:
myObject.myMethod('imageCheck', function () {
var image = new Image();
image.onerror = function() {
returnCallback(false);
};
image.onload = function() {
returnCallback(true);
};
image.src = 'http://www.example./image.jpg';
});
function returnCallback(bool){
//do something with bool
}
本文标签: javascriptHow to return from an async functionStack Overflow
版权声明:本文标题:javascript - How to return from an async function - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745655019a2161546.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论