【发布时间】:2020-06-06 18:34:27
【问题描述】:
在我的eventPage.js(background, persistent=false):
chrome.runtime.onMessage.addListener(async function(request, sender, sendResponse) {
await new Promise((resolve, reject) => {
chrome.downloads.search({id: files[i]}, function (item) {
sendfiles.push(item[0].id);
resolve();
});
});
console.log(sendfiles); // contains (item[0].id), means it did wait.
sendResponse("hello"); // Should send hello back
return true;
});
在我的popup.js:
chrome.runtime.sendMessage("",(response) => {
alert(response); // alerts undefinded instead of hello
});
我的错误:
Unchecked runtime.lastError: The message port closed before a response was received.
正如代码的cmets中所写,它应该用"hello"响应来自popup.js的请求,但是因为它在Promise中等待,所以我收到错误并且response变成@987654331 @ 之前我可以sendResponse 我的"hello" (我猜这就是发生的事情)..
【问题讨论】:
-
我们不能将此问题标记为重复问题,因为以下问题没有可接受的答案,但它看起来接近您的问题? chrome.runtime.onMessage response with async await
标签: javascript google-chrome-extension promise async-await