Welcome Guest, Not a member yet? Register   Sign In
returning a message from a background script
#2

(This post was last modified: 03-31-2018, 02:30 AM by richb201.)

I did some more searching and think I understand the issue better. 
1. My popup script sends a message to my background script to get some info xhr from the server.
2. My background script gets a token asynchronously to check for authentication. 
3. When I get an onload and readyState=200 (ok)  from the token request I call a function called send_to_backend() which gets the data I need from my server asynchronously. Send_to Backend is not called asynchronously, but inside it it has an asynch call to the server. 
4. My call to Send_to_backend() returns immediately to the call within the anon onMessage function, before this is before the asynch response from the server is ready. 
5. I immediately send the "uncooked response" back to the popup script. 
6. If instead i try to send the message back to the popup from within send_to_backend, sendResponse is not defined. I guess this is because I am not within the onMessage function?

The reason I created the send_to_backend function was to get out of "callback hell" and to make the code readable. But send_to_backend is not a callback itself, it is a function, and of course, is not blocking either.

Any ideas on how to deal with this? If I could just send the message from send_to_backend() to the popup script things seem to be working OK.
proof that an old dog can learn new tricks
Reply


Messages In This Thread
RE: returning a message from a background script - by richb201 - 03-31-2018, 01:41 AM



Theme © iAndrew 2016 - Forum software by © MyBB