KeithHenry / chromeExtensionAsync

Promise wrapper for the Chrome extension API so that it can be used with async/await rather than callbacks
MIT License
228 stars 32 forks source link

The message port closed before a response was received #29

Open Garito opened 4 years ago

Garito commented 4 years ago

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

santriseus commented 4 years ago

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.

Garito commented 4 years ago

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

RomanistHere commented 3 years ago
const asyncFunctionWithAwait = async (request, sender, sendResponse) => {...}

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

    return true
})

worked for me

seansfkelley commented 3 years ago

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.