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.