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
Add a ment  | 

1 Answer 1

Reset to default 6

you 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
Add a ment  | 

1 Answer 1

Reset to default 6

you 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