I'm starting a new project and want to try create this kind of 'modular' style to it. So far I've just used promises to load each file and was looking for some input into if there is any way to improve this.
Basically the app will start, and it will start loading each module, it should load each app, if any fail then it'll output the error and then terminate the process. If there's anything I could do better or maybe a simpler way I'd love the input
Below is the basic structure
app.js
/modules/index.js
/modules/test1.js
/modules/test2.js
app.js
modules/index.js
test1.js
Ignore the code inside test1.js, it's all for testing, all of this code works fine as is and will successfully return each resolve when they've all done loading. Only looking to see if this structure and loading can be done any easier/simpler than what I've done here.
Basically the app will start, and it will start loading each module, it should load each app, if any fail then it'll output the error and then terminate the process. If there's anything I could do better or maybe a simpler way I'd love the input
Below is the basic structure
app.js
/modules/index.js
/modules/test1.js
/modules/test2.js
app.js
Code
global.server = {};
console.log('Starting test')
require('./modules/index.js');modules/index.js
Code
server.test1 = require('./test1.js');
server.test2 = require('./test2.js');
console.log('Loading modules');
function loadModules(){
Promise.all([server.test1.start(), server.test2.start()]).then(function(values){
console.log(values);
}).catch((e) => { console.log(e)});
}
loadModules();test1.js
Code
module.exports = {
'start': function(){
return new Promise((resolve, reject) => {
let test = Math.random();
setTimeout(() => {
if(test >= 0.1){
resolve('Test 1 Complete');
} else {
reject('Test 1 Failed');
}
}, 3000);
});
}
}Ignore the code inside test1.js, it's all for testing, all of this code works fine as is and will successfully return each resolve when they've all done loading. Only looking to see if this structure and loading can be done any easier/simpler than what I've done here.


![[Image: 5hqc92A.jpg]](https://i.imgur.com/5hqc92A.jpg)
