1

I have a sendMessage method where I am supposed to get a response back from my background script.

However, sendMessage doesn't return anything, and to get the response, I have to use their built-in responseCallback function

chrome.runtime.sendMessage({ request: 'requestvalue' }, response => {
  console.log(response);
})

Is there a way for me to get my "response" value as a return value so that I can use it later?

something like this...

const response = chrome.runtime.sendMessage({ request: 'requestvalue' });
Toxnyc
  • 1,350
  • 7
  • 20
  • 1
    The API is asynchronous so you must use the value inside the callback. You can assign it to a global variable, of course. Or you can load [Mozilla's WebExtension polyfill](https://github.com/mozilla/webextension-polyfill) and switch to async/await syntax. – wOxxOm Apr 12 '19 at 18:57
  • 1
    @wOxxOm Thank you. I ended up wrapping `responseCallback` in a Promise. – Toxnyc Apr 12 '19 at 22:19

0 Answers0