다수의 Service를 동기적으로 호출해야 하는 경우, Array를 활용하면 유연한 구조를 설계할 수 있다.
initSvList() {
this.svList = [
{
svId: 'initPage',
svDv: 'init',
call: async () => {
await this.initPage();
},
},
{
svId: 'callFunc1',
svDv: 'call',
call: async () => {
await this.callFunc1();
},
},
{
svId: 'callFunc2',
svDv: 'call',
call: async () => {
await this.callFunc2();
},
},
];
this.callSvList(this.svList); // Service 호출
},
async callSvList(list) {
for (const it of list) {
console.log(`${it.svId} start!`);
await it.call();
}
},
initPage start!
callFunc1 start!
callFunc2 start!
// svId 가 'callFunc2' 인 Service만 호출
const onlyCallFunc2SvList = this.svList.filter(sv => sv?.svId === 'callFunc2');
this.callSvList(onlyCallFunc2SvList);
// svDv 가 'init' 인 Service만 호출
const onlyInitSvList = this.svList.filter(sv => sv?.svDv === 'init');
this.callSvList(onlyInitSvList);
{
svId: 'exceptionSample',
scDv: 'call',
call: async () => {
await this.callExceptionSample();
}
breakPoint: () => {
return this.isBreakPoint();
},
validate () => {
return this.isValidate();
},
}
async callSvList(list) {
for (const it of list) {
if (it?.breakPoint && it?.breakPoint()) {
break;
}
await it.call();
if (it?.validate && it?.validate()) {
await it.call();
}
}
},