1

I am having an issue executing the callback supplied by sendMessage, within an asynchronous function. Here is my senario:

popup.js

chrome.runtime.sendMessage('hello from popup', function(res){
    alert(res);
});

Background.js

chrome.runtime.onMessage.addListener(function (msg, sender, sendResponse) {
    setTimeout(function () {
        sendResponse('hi from background');
    }, 300);
});

The sendResponse called from the background script, does not fire when its inside the timeout (or any asynchronous method). The callback's alert never gets executed. Outside of the timeout, it fires just fine (the alert shows the response). There are no errors in the background scripts console, or in the popup scripts console. Perhaps this is a bug?

levi
  • 23,693
  • 18
  • 59
  • 73
  • alert is not available inside extension pages? – kzahel Feb 03 '14 at 03:48
  • Why doesn't anyone read the docs any more ? :( – gkalpak Feb 03 '14 at 06:25
  • @ExpertSystem I hear ya. But cmon, this is the classic addListener(callback) – levi Feb 03 '14 at 18:04
  • @levi: Levi uses "classic addListener(callback)". Levi faces problem. **Case 1**: Levi spends 5mins to write a question on SO. A bunch of people spend 30 secs to read through the question. Rsanchez spends 2 minutes to look up the duplicate answer and post a comment. Levi spends 1.5 minutes answering to comments. **Case 2**: Levi spends 2 minutes reading the documentation on the particular method. Problem solved ! (Well, do the math yourself ;)) – gkalpak Feb 03 '14 at 19:09
  • @ExpertSystem point taken – levi Feb 03 '14 at 20:13

0 Answers0