We all know term called Recursion. That is same we are doing in below example to loop through multiple functions which should execute one after another.
This is one of the ways we can achive function serialization and can be usfull to your application.
const objFunctions = [
{ func: function1, args: args1 },
{ func: function2, args: args2 }
]; //We defined functions that we want to call one by one.
function LoopFunctionOneByOneCallAfterOneAnother(objFunctions) {
if (!objFunctions) process.exit(0); // finished
ExecuteSingleFunctionCall(objFunctions, function (result) {
// continue AFTER callback
LoopFunctionOneByOneCallAfterOneAnother(objFunctions.shift());
});
} // notice we called same function under this function which has parameter to get next object from objFunctions.
function ExecuteSingleFunctionCall(objFunction, callback) {
const { args, func } = objFunction;
func(args, callback);
} // Supportive function to execte single function.
LoopFunctionOneByOneCallAfterOneAnother(objFunctions.shift()); // In the end we call our function.
So this whole block will execute in serialized manner.
Above referenced from site https://nodejs.dev/en/learn/asynchronous-flow-control/, you can find more usefull ways over here.