I am having an issue of asynchronicity (I believe). sendResponse()
in contentscript.js does not wait for getThumbnails()
to return
The callback of onMessage should return true 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 because Chrome extensions API doesn't support Promise and hence can't resolve it.
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
}
});