5

I need to have a value returned by and event handler into the function that initiates the event in the first place, just to return the value that will be received only in the event handler. I believe there should be an easy way to accomplish this or to attack the problem from a different angle.

What I actually need to do is:

  1. Create a new img DOM object to feed dataURL into (the dataURL is coming from canvas.toDataURL())
  2. img.onload will let me return the dataURL

I'm using jQuery if that helps somehow but don't have to of course.

In my understanding I can't rely on the img to have the data immediately, and it feels stupid to use timeouts to wait for the event to fire and use some global variable, especially since a few might be processed in parallel.

For even more background, this is all for resizing images before (multi-)uploading them with HTML5 engine.

Thanks even for reading, and for any tips of course :)

Thanks, Igor

Igor R
  • 625
  • 2
  • 10
  • 24
  • Nothing, just wrote the event handler and then realized I need the value in the "parent" function. I realize I can loop with timeouts, but intuition tells me there should be something smarter :) – Igor R Jan 17 '12 at 21:12
  • 2
    A promise library might be worth looking into: https://github.com/kriskowal/q – thetallweeks Sep 12 '14 at 18:14
  • 1
    that's a very old one, but might be a useful comment for someone who stumbles upon this question. I use Bluebird these days for promises. Reviewing the question made me realize much progress had been made, thanks @thetallweeks for the Friday refresher )) – Igor R Sep 12 '14 at 18:41

2 Answers2

9

It doesn't make sense to return values from an event handler. Events happen when they happen, and the handler is called by the browser at that time. Thus, there's no code expecting any return value; there's nowhere for it to go.

Instead, simply do whatever it is you need to do inside the event handler.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • I need to return the value from the function (F) that constructed the object from which the event fires, to assign it to a variable that called F in the first place. Sorry if it's confusing :) – Igor R Jan 17 '12 at 21:19
  • I know what you're saying, but it's simply not possible to do that. JavaScript in web browsers is an asynchronous environment; you can't make a function "wait" for something to happen. The way to structure your code, therefore, is to embrace the asynchronous nature, and instead of writing a function that expects a return value, have it *pass in* a handler function of its own that will be called by the "onload" handler of the image. – Pointy Jan 17 '12 at 21:27
  • 1
    I think I'm getting there... I need to pass another handler into the original handler to execute it there when the img is loaded... – Igor R Jan 17 '12 at 21:30
  • 1
    Thanks for helping me through, it did make me think ) In addition there's this nice thread for anyone who has a similar problem: http://stackoverflow.com/questions/2993563/how-do-i-return-a-variable-from-google-maps-javascript-geocoder-callback – Igor R Jan 17 '12 at 21:30
0

As mentioned it is not possible to return value from an event, I am a beginner to js and I was stuck in this problem until I found a dummy work around. you can create an html element a textarea for example, and write your return value to it via .innerText, then access it wherever you want using .textContent Keep in mind that it will be retrieved as a string, so in your case you will need to convert it. you can always hide textArea.

hasu33
  • 1,891
  • 2
  • 4
  • 8