I have a chrome extension and I want the user to navigate to some page on button click, I get the URL from the server so I need to wait for the promise to resolve to know where to redirect the user to.
I tried doing it in the then
section of the promise but realized it doesn’t work because window open works only when triggered on the main thread as a result of user action (as explained here).
One solution I came up with is using setInterval
and for some reason it works. I don’t know why and would love to understand better, maybe it’s flaky and I shouldn’t use it.
My code looks somewhat like this
const onClick = () => {
const urls: string[] = []
somePromise.then((url) => {
urls[0] = url
})
setInterval(() => {
const possibleURL = urls[0];
if (possibleURL) {
window.open(possibleURL, '_top')
}
}, 500)
}
Thanks in advance