Order of statement execution between background scripts and content scripts

My question concerns the order of statement execution between the background script and a content script when a function in one invokes a function in the other.

For example, suppose a function in the background script posts a message to the content script using portFromCS[id].postMessage(obj), which invokes a function in the content script, but there are still lines of code following the postMessage statement in the background script’s function.

Will execution move from the background script to the content script at the point of posting the message and begin again after the function in the content script invoked by that message completes/returns, or can both functions run concurrently? Does the background script even know that the function in the content script completed, apart from the content script posting a return message?

The fuller scenario is that a user action in the page is picked up by an event listener in the content script which posts a message to the background script requesting it to do something, such as retrieve data, after which the background script posts a message to the content script informing it of what to do with the data it is passing back.

I realize it doesn’t matter much in the respect that the scripts cannot share objects directly; however, I’m trying to figure out how to handle error catching properly; and in some cases timing may be important, although I’m not certain about that yet.

Thank you.

The two scripts are in separate processes, so no, execution does not move from one place to the other. Instead they each have an event loop. If you want to wait for an answer, you would have to send a message back and wait for that message. Or in the error catching case, you’d likely send a message whenever you see an error so you can display it in the UI to the user.

Thank you. I think you have answered about 90% of my questions here. I appreciate it; you’ve been a great help.