The message port closed before a response was received
Hi! I get this error:
Uncaught (in promise) Error: The message port closed before a response was received.
at chrome-extension-async.js:46
trying to send a message to background.js like:
await chrome.runtime.sendMessage({ action: 'toggleIcon', value: dots });
Can someone help figuring out what is the reason or how to use this feature properly?
Thanks
From my investigation you always need to call sendResponse callback in corresponding onMessage.attachListener to avoid this error. Or you could return some promise from it if if you want to handle message in async manner. https://developer.chrome.com/extensions/runtime#event-onMessage Probably before some moment of time calling back was not mandatory.
There is a big discussion in other plyfill library: https://github.com/mozilla/webextension-polyfill/issues/130 As I could see pollyfill authors just ignored this type of errors at the pollyfill level.
Yeah Can you show an example? I thought this library's main point is to avoid the callback hell...
const asyncFunctionWithAwait = async (request, sender, sendResponse) => {...}
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
asyncFunctionWithAwait(request, sender, sendResponse)
return true
})
worked for me
It appears that onMessage is not fully supported by this library. I switched to https://github.com/mozilla/webextension-polyfill which seems to cover more of the API surface, including onMessage.