简体   繁体   English

javascript 异步函数,调用者不等待。 清单 V3

[英]javascript async function, caller not waiting. manifest V3

Do NOT delete my question.不要删除我的问题。 I have scoured the web, the documentations, I don't know why this is happening and deleting the question does NOT help.我已经冲刷网页,该文档,我不知道为什么发生这种情况,并删除问题没有帮助。 There no other similar questions to mine.没有其他与我类似的问题。

Here goes.开始。 Below is how I beleive it should happen.以下是我相信它应该发生的方式。 But even with all the async away promis syntax I place there, it doesn't happen.但即使我把所有的 async away promis 语法放在那里,它也不会发生。 When the async function is called, no one waits for it to finish.当异步函数被调用时,没有人等待它完成。 Idk what else to do except daisychain everything under the async function which I am trying to avoid.我想知道除了菊花链之外我试图避免的异步功能下的所有内容还能做什么。 Help please.请帮忙。

I have tried using tabs= await getcurrenttab() , but that causes an error saying it only works on async functions.我曾尝试使用tabs= await getcurrenttab() ,但这会导致错误,指出它仅适用于异步函数。 Which the listener is not.听众不是。

在此处输入图片说明

manifest.json清单文件

{
  "manifest_version": 3,

  "name": "DHL Helper",
  "description": "This helper helps DHL's quality of life, improves mental health.",
  "version": "1.0",

  "action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
  },
  "content_scripts": [{
  "matches":["*://servicenow.*.com/*",
            "http://*/*",
            "https://*/*"],
    "js": ["content_helper.js"]
  }],
  "permissions": [
   "activeTab"
   ],
  "host_permissions": [
    "http://www.blogger.com/",
    "*://*/*"
  ]
}

popup.js弹出窗口.js

document.addEventListener('DOMContentLoaded', function() {
    console.log('got tabs id');
    tab = getCurrentTab();
    console.log('Active tab ' + tab.id);
});    
async function getCurrentTab(){
    console.log('trying to get tabs id');
    let queryOptions = {active: true, currentWindow: true};
    return await chrome.tabs.query(queryOptions)
    .then((tabs) => {
        console.log('Obtained tab ID');
        console.log(tabs);
        return tabs[0];
    })
    .catch((Error) => {
        console.log('it failed');
        console.error;
        return;
    })
}
document.addEventListener('DOMContentLoaded', async function() {
    console.log('got tabs id');
    tab = await getCurrentTab();
    console.log('Active tab ' + tab.id);
});

Your approach was already correct, but if you want to use await in the function await, you also have to make the executing function asyncronous.你的方法已经是正确的,但是如果你想在函数 await 中使用 await,你还必须使执行函数异步。

user mentions attempting to use =await getCurrentTab() but encounters error that await is only for async functions...用户提到尝试使用=await getCurrentTab()但遇到错误,即 await 仅适用于异步函数...

adding async to event listener function fixed the problemasync添加到事件侦听器功能修复了问题

change this:改变这个:

document.addEventListener('DOMContentLoaded', function() {

to this:对此:

document.addEventListener('DOMContentLoaded', async function() {

tab not defined标签未定义

var tab = getCurrentTab(); var tab = getCurrentTab();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM