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

427
Visualizações
Nodejs: Fully executing websocket responses sequentially with async calls

Below is a simple example of what I'm currently working with: a websocket stream which does some asynchronous calls as part of the logic when consuming the incoming data. I'm mimicking async calls with a Promise-ified setTimeout function:

function someAsyncWork() {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve('async work done');
      }, 5);
    });
}
  
async function msg() {
    const msg = await someAsyncWork();
    console.log(msg)
}

const main = async() => {

    web3.eth.subscribe('pendingTransactions').on("data", async function(tx){
        console.log('1st print: ',tx);
        await msg();
        console.log('2nd print: ',tx);
    })
}

main();

Running the above results in a console output like so:

1st print:  0x8be207fcef...
1st print:  0x753c308980...
1st print:  0x4afa9c548d...
async work done
2nd print:  0x8be207fcef...

async work done
2nd print:  0x753c308980...

async work done
2nd print:  0x4afa9c548d...
.
.
.

I get what's happening here. The '1st print' is executed, followed by await-ing the async calls for each piece of data response. The '2nd print' is only executed after the 'async work done' occurs. However this isn't quite what I'm looking for.

My logic has conditionals in place, where each data response will first use a global variable to check for a condition, followed by some async work if condition is met. Problem is that there are instances where some data responses will go ahead and execute async work when they shouldn't have: Nodejs's event loop hasn't had a chance to transfer some previous data response's async calls from the callback queue to the call stack, as the stack was too busy processing new incoming data. This means the '2nd prints' haven't executed (where the global variable is updated) before new incoming data has been processed. I imagine the someAsyncWork is only resolved when there is a free period in the websocket with no data incoming.

My question is: is there a way to ensure full, sequential processing of each piece of new data? Ideally the console output would look something like this:

1st print:  0x8be207fcef...
async work done
2nd print:  0x8be207fcef...

1st print:  0x753c308980...
async work done
2nd print:  0x753c308980...

1st print:  0x4afa9c548d...
async work done
2nd print:  0x4afa9c548d...
.
.
.
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

You can have a queue-like promise that keeps on accumulating promises to make sure they run sequentially:

let cur = Promise.resolve();

function enqueue(f) {
    cur = cur.then(f);
}

function someAsyncWork() {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve('async work done');
      }, 5);
    });
}
  
async function msg() {
    const msg = await someAsyncWork();
    console.log(msg);
}

const main = async() => {

    web3.eth.subscribe('pendingTransactions').on("data", function(tx) {
        enqueue(async function() {
            console.log('1st print: ',tx);
            await msg();
            console.log('2nd print: ',tx);
        });
    })
}

main();
over 4 years ago · Santiago Trujillo 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