Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

180
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda