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

181
Visualizações
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
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