Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

283
Visualizações
Chrome extension – async onMessage response

I'm having an issue with chrome handling. Here is how I handle communication between my content script, and my background service worker.

Content script

const sendMessage = (msg) => {
  return new Promise((resolve) => {
    console.log('1');
    chrome.runtime.sendMessage(msg, (data) => {
      console.log('4');
      resolve(data);
    });
  });
};


sendMessage.then(...)

Background listener

chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
  console.log('2');
  myAsyncHandler().then((data) => {
    console.log('3');
    sendResponse(data);
  });
  return true;
});

I have this whole thing running in a new tab, and now:

  1. When I open my first new tab, everything works as expected – 0 issues – log statements in order: 1, 2, 3, 4
  2. When I open my second tab (2 tabs now running), the issue appears as the sendMessage returns null. Log statements appear in order 1, 2, 4, 3.

I've tried to debug this, but no luck at all. I've even tried to log chrome.runtime.lastError when I received null in the sendMessage callback – but that's undefined.

I'd be grateful for all hints on what might be causing it!

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda