fetch-event-source icon indicating copy to clipboard operation
fetch-event-source copied to clipboard

Variable outside await fetchEventSource is undefined

Open ducknificient opened this issue 1 year ago • 2 comments
trafficstars

The code is working as expected

onmessage(ev) {
       console.log(ev.data);
}

However, when using the ev.data , i can't access variable outside fetchEventSource. How to handle it ?

ducknificient avatar Mar 03 '24 01:03 ducknificient

The problem right there @ducknificient is that the closure doesn't know about the variables outside fetchEventSource methods. I handled that issue by using var variables declared somewhere else outside fetchEventSource call. Those variables will be available on the global scope and then you'll be able to use them inside those events. Binding the method call didn't work for me.

rcardona-eb avatar Jun 24 '24 20:06 rcardona-eb