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

179
Views
How to write Node callbacks with multiple parameters without nesting?

I'm very confused about how to make async function calls to callbacks that require multiple parameters that the initial function might not have access to. For example,

I call getData with count equal to 3 and callback being a second function, parseData, which itself requires the data and a dataSchema.

 const allData = [2, 2, 2, 2, 2]

function getData(count, callback) {
    if (count <= allData.length) {
        return callback(null, allData.slice(0, count));
    }
    else console.log("no");
}

function parseData(data, dataSchema, callback) {
    // parse data according to schema
}    

getData(3, parseData);

getData doesn't need to know about dataSchema, so how do I provide that information to parseData while maintaining async flow?

One option would be:

getData(3, (err, data) => {
    if (err) console.log(error);

    else parseData(data, mySchema);
};

dataSchema might be the structure of the object that we want to organize the data in, an object with enumerations as keys vs a plain array, for example. It's just meant to illustrate that the callback requires additional info that the initial function doesn't need and shouldn't have.

But if there are several operations this approach will lead to complex nesting.

EDIT: I know async/await and promises are a thing but I wanted to understand how this was done before.

about 4 years ago · Juan Pablo Isaza
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!