sendResponse 不等待非同步函數或Promise 的Resolve
問題:
問題:
問題:
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => { if (msg.message === "get_thumbnails") { processMessage(msg).then(sendResponse); return true; // keep the messaging channel open for sendResponse } }); async function processMessage(msg) { console.log('Processing message', msg); // ................. return 'foo'; }
要將偵聽器相容:
if ('crbug.com/1185241') { // replace with a check for Chrome version that fixes the bug const {onMessage} = chrome.runtime, {addListener} = onMessage; onMessage.addListener = fn => addListener.call(onMessage, (msg, sender, respond) => { const res = fn(msg, sender, respond); if (res instanceof Promise) return !!res.then(respond, console.error); if (res !== undefined) respond(res); }); }
chrome.runtime.onMessage.addListener(async msg => { if (msg === 'foo') { const res = await fetch('https://foo/bar'); const payload = await res.text(); return {payload}; } });
以上是為什麼 Chrome 擴充功能的 onMessage 監聽器中的 `sendResponse` 不等待我的非同步函數?的詳細內容。更多資訊請關注PHP中文網其他相關文章!