Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response

I am finding the 'Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received' error in the background.js. My chrome extension works but the dev tools displays a lot of these errors on background.js

I recently migrated my chrome extension from Manifest V2 to Manifest V3 and since then I am finding the error.

This is the place from where the error seems to be arising :

const browser = chrome || browser;

// Listen for messages from content script
browser.runtime.onMessage.addListener(
    function (request, sender, sendResponse) {
         var frameId = sender.frameId || 0;

            if (request.type == "connect") {               
                connect(sender.tab, frameId);
                sendResponse({ success: true });
            } else if (request.type == "disconnect") {                
                disconnect(sender.tab, frameId);
                sendResponse({ success: true });
            } else if (request.type == "send") {
                sendNativeMessage(request.data, sender.tab);
                document.addEventListener('nativeMessage',
                    function handleNativeMessage(e) {
                        sendResponse(e.detail);
                        document.removeEventListener(e.type, handleNativeMessage);
                    }, false);

               sendResponse({});                
               return true;
            }               
    });

The sendresponse is processed in ContentScript :

const browser = chrome || browser;
self.addEventListener("message", function(event){
    var request = event.data;
    if(request != null && request.type == "SendMessage")
    {
        ProcessNativeMessage(request.data);
    }
    
});

function ProcessNativeMessage(nativeMessageData) {
  var request = new Object();
  request.data = nativeMessageData;
  browser.runtime.sendMessage(request,handleExtensionResponse);
}

function handleExtensionResponse(value)
{
    //alert(value);
};

I played around by returning true, returning false, returning undefined, sending empty response and even disabling the other extensions but none of these changes worked.

I even tried changing the code with async/await as suggested here but it makes no difference.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Does anything change if you rewrite the code like this?
Anyway I can't reproduce an extension with native messaging, sorry.
P.S. Before any scientist decides to downvote me badly, I want to ensure that if the solution doesn't work I'm willing to cancel the answer.

const browser = chrome || browser;

var globSendResp, globReqTypeSend;      //(*)

// Listen for messages from content script
browser.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    var frameId = sender.frameId || 0;
    globReqTypeSend = false;            //(*)
    if (request.type == "connect") {               
        connect(sender.tab, frameId);
        sendResponse({ success: true });
    } else if (request.type == "disconnect") {                
        disconnect(sender.tab, frameId);
        sendResponse({ success: true });
    } else if (request.type == "send") {
        globReqTypeSend = true;         //(*)
        globSendResp = sendResponse;    //(*)
        sendNativeMessage(request.data, sender.tab);
        //sendResponse({});             //(*)
        return true;
    }
});

document.addEventListener('nativeMessage', function handleNativeMessage(e) {
    if (globReqTypeSend)
        globSendResp(e.detail)
}, false);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!