chrome extension - sendResponse not waiting for async function [duplicate]
The callback of onMessage should return a literal true
value (documentation) in order to keep the internal messaging channel open so that sendResponse can work asynchronously.
The problem is, your callback is declared with async
keyword which means it returns a Promise
so it can't return a literal true
value. Chrome extensions API doesn't support Promise in the returned value of a callback so it's just ignored.
Use a standard function callback and a nested async IIFE:
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
if (request.message === "get_thumbnails") {
(async () => {
const payload = await getThumbnails();
console.log("thumbPayload after function:", payload)
sendResponse({payload});
})();
return true; // keep the messaging channel open for sendResponse
}
});