javascriptgoogle-chrome-extensionchromiumsendmessage

Async response to onMessage returns null


I am trying to get an asyc response with the chrome messaging system but it always returns the literal value null. In the case below the listener gets the value and fetches the key but the value in response is always null. Even if I take the call to getKey() and replace it with a static string response is null.

If I respond synchronously with a static string it works as expected. What am I missing about the async response pattern? The sender is a service worker and the reciever is a chrome extension.

(async () => {
        const response = await chrome.runtime.sendMessage({greeting: "hello"});
        console.log("RESPONSE" + response);
      })();

The response logic:

chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  (async function () {
    var key = await getKey();
    sendResponse(key);
  })();

  // return true to indicate you want to send a response asynchronously
  return true;
});

Solution

  • Ultimately the answer here was that we had a different listener that was causing a response to be sent but it wasn't using the sendResponse() object. We thought that because it wasn't using sendResponse that we would be safe but that was incorrect.