The code's purpose is to call to some function async given some condition was true. I've using the following code which is working as expected. Is there a way to write it better, maybe by not using the promise push array?
var _run = (req, res, oModule, oConfig, urlPath,action) => {
var aPromises = [];
//requested action to call
for (var provider in oConfig.providers[0]) {
if ((oConfig.providers[0][provider].val === action))
{
var fnName = oConfig.providers[0][provider].function;
//Run on provided actions
if (typeof oModule[oConfig.providers[0][provider].function] === "function") {
try {
logger.info(`Function ${fnName} is called `);
aPromises.push(oModule[fnName](req, res));
}
catch (err) {
logger.error(`Error returned ${err}`);
}
}
}
}
return Promise.all(aPromises);
};
update:
This is the value of config object and in the code I need to call to the function according to the path I got...
{
"providers": [
{
"run": {
"path": "command1",
"function": "fn1",
},
"save": {
"path": "test2",
"function": "fn2",
}
}
]
}
provider
isn't used, so the loop looks meaningless. \$\endgroup\$