The reason is that sendMessage is now promisified internally, so we can `await` it, but the byproduct is that when we don't specify a callback ourselves, it is added internally anyway in order for the call to return a Promise, which means that since we don't call sendResponse in onMessage, the API will think that it was us who made a mistake of using a callback and not providing a response, and report it as such.
Two workarounds for extensions:
1. Always call sendResponse() inside chrome.runtime.onMessage.
// sender
chrome.runtime.sendMessage('test');
// receiver
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
doSomethingWithoutResponding(msg);
sendResponse();
});
2. Suppress this specific error.
Also patch the API's inherent lack of a callstack prior to the call (a bug in itself BTW).
// sender
sendMessage('foo');
sendMessage('foo').then(res => whatever(res));
await sendMessage('foo');
function sendMessage(msg, options) {
const err1 = new Error('Callstack before sendMessage:');
return new Promise((resolve, reject) => {
chrome.runtime.sendMessage(undefined, msg, options, res => {
let err2 = chrome.runtime.lastError;
if (!err2 || err2.message.startsWith('The message port closed before')) {
resolve(res);
} else {
err2 = new Error(err2.message);
err2.stack += err1.stack.replace(/^Error:\s*/, '');
reject(err2);
}
});
});
}