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