chromeExtensionAsync icon indicating copy to clipboard operation
chromeExtensionAsync copied to clipboard

The message port closed before a response was received

Open Garito opened this issue 5 years ago • 4 comments

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

Garito avatar Oct 05 '20 16:10 Garito

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.

santriseus avatar Oct 07 '20 14:10 santriseus

Yeah Can you show an example? I thought this library's main point is to avoid the callback hell...

Garito avatar Oct 07 '20 16:10 Garito

const asyncFunctionWithAwait = async (request, sender, sendResponse) => {...}

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
    asyncFunctionWithAwait(request, sender, sendResponse)

    return true
})

worked for me

RomanistHere avatar Dec 22 '20 08:12 RomanistHere

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.

seansfkelley avatar Feb 23 '21 23:02 seansfkelley