Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sendResponse undefined when executing browser.runtime.onMessage.addListener((message, sender, sendResponse) #77

Closed
ghost opened this issue Nov 21, 2017 · 3 comments

Comments

@ghost
Copy link

ghost commented Nov 21, 2017

Am I missing something here? According to the docs (https://developer.mozilla.org/en-US/Add-ons/WebExtensions/API/runtime/onMessage), onMessage should always have a third 'sendResponse' function parameter. The code around https://github.com/mozilla/webextension-polyfill/blob/master/src/browser-polyfill.js#L330 suggests that I need to define the listener as following:

browser.runtime.onMessage.addListener((message, sender) => {
    return new Promise((resolve, reject) => {

    })
})

Doesn't this break for browsers without the polyfill? (e.g. Firefox/Edge)

@LilDevil
Copy link

@jvanveen, I think the part you're missing is in the docs, where it says

To send an asynchronous response, there are two options:

  • return true from the event listener. This keeps the sendResponse function valid after the listener returns, so you can call it later.
  • return a Promise from the event listener, and resolve when you have the response (or reject it in case of an error).

The code you posted is the 2nd option. This works just fine in Firefox, as well as in Chrome with the polyfill.

If you want a more synchronous version:

browser.runtime.onMessage.addListener((message, sender) => {
  return Promise.resolve("reply");
})

But you are correct that the callback version (sendResponse in the Mozilla docs) does not polyfill correctly.

@LilDevil
Copy link

This appears to be a duplicate of #16 where there is lots of interesting discussion of the issue.

@Rob--W
Copy link
Member

Rob--W commented Jul 5, 2018

Closing. This was fixed by #97 and will be released in the next version (0.3.0, see #88).

@Rob--W Rob--W closed this as completed Jul 5, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants