Team/난감했던 이슈들

[ JAVASCRIPT ] forEach Async

HEON.D 2024. 2. 2. 03:33

forEach 는 await 대기하지 않음

res.data.result_data['progressLists'].forEach(async row => {
            let index = await items.findIndex(i => i['IP_CODE'] == row['IP_CODE']);
            if (index > -1) {
              items[index]['schedules'].push(row);
              console.log(items[index]['schedules'][0]['PROGRESS_CODE']);
            }
});

대치

 for (const row of res.data.result_data['progressLists']) {
            const index = await items.findIndex(i => i['IP_CODE'] == row['IP_CODE']);
            if (index > -1) {
              items[index]['schedules'].push(row);
              console.log(items[index]['schedules'][0]['PROGRESS_CODE']);
            }
}