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

180
Visualizações
Pause function to wait for ajax request inside of a websocket

I have a WebSocket that adds people to a messaged list when they receive a message. I use a fetch to get information from the server to build the messaged list, but I cannot find an easy way to get the code to pause until the fetch completes. I cannot add await because the funciton is inside a websocket that isn't async. Does anyone have any suggestions on this?

        socket.on('receive_message', function(data) {
            let messaged_user_li = document.getElementById('messaged_user_li'+data['from_user']);
            console.log('messaged_user_li: '+messaged_user_li)
            if (messaged_user_li == null) {
                console.log('if fired')
                //THIS is the fetch I want to pause the function until complete. Note that the await here does not work since the funciton isn't async.
                await fetch('/get_messaged_user/'+from_user).then((response) => {
                    response.json().then((data2) => {
                        loadMessagedUser(data2[0].id, data2[0].avatar, data2[0].username, data2[0].last_seen);
                    });
                });
                messaged_user_li = document.getElementById('messaged_user_li'+data['from_user']);
            }
            console.log('messaged_user_li: '+messaged_user_li)
            let message_user = document.getElementById('message_user'+data['from_user']);

            let message_target = document.getElementById("message_target"+data['from_user']);
            if (messaged_user_li.classList.contains('active') == false) {
                messaged_user_li.classList.add('flash-message');
            }
            if (message_user != null) {
                data = `
                <li class="clearfix">
                    <div class="message-data text-right">
                        <span class="message-data-time">just now</span>
                    </div>
                    <div class="message other-message float-right">`+data['message']+`</div>
                </li>`;
                message_target.innerHTML += data;
                //Move scroller to bottom when message received
                myDiv = message_user.querySelector(".chat-history");
                myDiv.scrollTop = myDiv.scrollHeight;
            }
        });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Actually You can add async function in socket.on

socket.on('receive_message',async function(data) {

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