Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

54
Visualizações
How to wait till desired event is fired and only then return promise

I am opening a new window on my website where i have to wait till the user does a transaction on another website. I will then receive a postMessage call once the transaction is complete. How do i wait and only return once the even is fired?

This is a short example of the code:

async function anonTransaction() -> Promise {

const popupWindow = window.open("http://localhost:3000/")

let result = await new Promise((resolve, reject) => {
window.onmessage = (event) => {
        if(event.origin !== "http://localhost:3000"){
            reject()
        }
        else{
            console.log("resolve");
            resolve()
        }    
}});
}

/// on the opened website

let targetWebsite = window.opener;
targetWebsite.postMessage("message");
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I am wondering, why don't you just execute your logic within onmessage?

window.onmessage = (event) => {
    if(event.origin === "http://localhost:3000"){
        // execute your logic here
        myAsyncFunction();
    }  
};

If you really want a promise, you can try and create a global onmessage event listener which rejects or resolves a promise by saving the referrences to resolve and reject:

let resolve;
let reject;

let result = await new Promise((res, rej) => {
    resolve = res;
    reject = rej;
});

window.onmessage = (event) => {
    if(event.origin !== "http://localhost:3000"){
        if(reject) reject()
    } else{
        console.log("resolve");
        if(resolve) resolve()
    }    
};
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda