0

It appears the response is sent immediately (it logs in the console immediately). Is there a problem with the code or some other issue?

popup.js (browser action)

async function send() {
  const response = await chrome.runtime.sendMessage({...});
  console.log('response called', response);
}

background.js (service worker)

chrome.runtime.onMessage.addListener(e => onMessage(e));

async function onMessage(message) {
  return fetch('https://example.com/')
  .then(response => response.json())
  .then(response => {
    console.log('json called', response);
    return response;
  })
  .catch(error => {
    console.log('error called', error);
  });
}

Result

response called undefined
json called {...}
erosman
  • 7,094
  • 7
  • 27
  • 46
  • @wOxxOm Thank you. It answers the question that it is a bug. Sadly, the callback solution doesn't suit my situation (cross -browser compatible code). – erosman Nov 24 '22 at 19:48
  • Firefox supports callback-based sendResponse. Which browser doesn't? – wOxxOm Nov 24 '22 at 21:27
  • @wOxxOm Firefox `chrome` namespace API in MV3 will be promise based. It is callback-based in MV2. – erosman Nov 25 '22 at 08:44
  • 1
    The general architecture is a different matter. The point I make is that sendResponse will work in Firefox. – wOxxOm Nov 25 '22 at 09:33

0 Answers0