I am trying to wait for the listener to listen to all messages before moving on to console.log("Done") using await but it is not happening. What am I missing.
const f = async (leftPaneRowEle,index) => {
leftPaneRowEle.scrollIntoView()
leftPaneRowEle.children[0].click()
console.log('downloadBtn clicked for', leftPaneRowEle)
const listener = (msg: any) => {
console.log('frontend msg:', msg)
}
const port = Browser.runtime.connect()
port.onMessage.addListener(async (msg, sender) => {
console.log("BG page received message", msg, "from", sender);
listener(msg)
});
await port.postMessage({ question: "Can you " + allTFBs[leftpaneindexes[index]] + ":" + desc })
return async () => {
await port.onMessage.removeListener(listener)
await port.disconnect()
}
}
const en: HTMLElement = document.querySelectorAll('#extensionListTable tbody')[0] as HTMLElement
const leftPaneRowEle0 = en.children[leftpaneindexes[0]]
await f(leftPaneRowEle0,0)
console.log("Done")
PS: My approach is inspired by this answer
Try this