With NodeJS version 16.14.0 I'm trying to migrate from a promise .then().catch to a async/await management.
The problem is that with the first method, I was able to chain multiple functions sequentially, with the same big object passed through each of it (with return resolve(bigObject) and each function taking as input the same bigObject). I was doing this because I'm able to manage micro-errors inside the data (so no catch involved), returning the user a full list of errors:
var commonData = {
data1: ...
data2: ...
data3: ...
data4: ...
data5: ...
data6: ...
};
return function1(commonData)
.then(function2)
.then(function3)
.then(function4)
.then(function5)
.then(function(result) {
//Inside this .then, I'd have the "result" variable as result of the chain
})
.catch(error => { return reject(error); });
How can I do it with async await system? This is what I've tried so far:
var commonData = {
data1: ...
data2: ...
data3: ...
data4: ...
data5: ...
data6: ...
};
commonData = await function1(commonData);
commonData = await function2(commonData);
commonData = await function3(commonData);
commonData = await function4(commonData);
commonData = await function5(commonData);
//I was expecting to have inside "commonData" the same situation
//as "result" in the chain above
Is there another way to chain functions with the same big object passed through that I'm missing?
EDIT:
2 of the functions I'm using in this part...they are designed this way because they are being called in other parts of the file too, I couldn't use them locally
//function 1
async function function1(commonData) {
try {
var data1 = await models.data1.findAll({where: {Domain_ID: [13,15]}})
data1.forEach(function(dat) {
commonData.data1.push(dat.dataValues);
});
return commonData;
} catch (error) {
throw error;
}
}
//function2
async function function2(commonData) {
try {
var data2 = await models.data2.findAll();
data2.forEach(function(dat) {
commonData.data2.push(dat.dataValues);
});
return commonData;
} catch (error) {
throw error;
}
}