Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

418
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!