0

I have a function like the following, but the function calling this one return undefined, I don't know why.

$.fileReader_g = function(file){
        var reader  = new FileReader(); 
        reader.onloadend = function(){
                return {
                        result:     reader.result, 
                        size:       file.size, 
                        name:       file.name
                }
            }

        if (file){
                reader.readAsDataURL(file);
        } else {
                console.log('Upload Failed');
        }
    }

The above is called by the following.

$(document).on('change', '.album_art_select', function(){
        var cow = $.fileReader_g($(this)[0].files[0]);
        console.log(cow);
    });

Where am I missing this?

Relm
  • 7,923
  • 18
  • 66
  • 113

1 Answers1

1

You define two functions.

One you assign to $.fileReader_g. It does not have a return statement and will always return undefined.

The other you assign to reader.onloadend. It does have a return statement, but nothing is going to pay any attention to it.

See also How to return the response from an asynchronous call?.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335