I have a list that needs to be iterated through, with each iteration dependent on the completion of the previous one. Each iteration returns a promise.
Currently, my code processes the list quickly without waiting for the promises to resolve:
this.records.forEach(item => {
this.myService.doFirstTask().then(res => {
if (res) {
this.myService.doSecondTask().then(res => {
if (res) {
// do something.
}
});
}
});
});
The issue is that I need to wait for myService.doSecondTask()
to finish before moving to the next item in the list.
I understand that using an async
function with await
could solve this problem, but I'm unfamiliar with it.
Could someone please offer guidance on how to wait for a nested promise to resolve before iterating through the .forEach loop?
Should I use async-await
in the .doSecondTask()
function as the solution?