Foreground and background scripts use a messaging system to communicate back and forth. But sometimes one side starts an asynchronous operation that the other side should pledge to honor. This requires making Promises work across this communications channel.
Does anyone have any idea how to do this? Since I don’t understand the details of the internal implementation of Promises, and can barely make them work, I haven’t the foggiest idea how to accomplish this.
Perhaps a use case will motivate this question better. Since console.log() does not work reliably in the WebExtensions environment (at least, that is my experience over several days of experimentation), it would be nice to display modal notification boxes containing debugging messages, analogous to debugging by using the alert() function.
This is done by browser.notifications.create(), but this function requires some Promise wrapping to wait for 500 msec after the box is created so it can be displayed before moving on to doing something else, and to wait for 3000 msec or more after the box is created before destroying it (in case the user fails to click the box to dismiss it). I’ve posted my solution to this problem elsewhere (https://stackoverflow.com/questions/53869972/how-to-wrap-webextensions-notifications-in-a-promise).
So, say the foreground script needs to send you some text. Since foreground scripts are not allowed to create their own windows, and probably should not create their own modal DIVs either, the foreground script sends a message to the background script to display the box. But when I try to implement this, the box displays only for a fraction of a second, then disappears. The idea that this is a two-environment asynchronous process has not been implemented, so waiting is not coordinated between the two environments.
We need a way of envisioning and supporting multi-environment Promises, where the environments can only be coordinated via the browser.runtime messaging facility.