Reputation: 3626
I have an array of string where I would like to call an async function on each string. What is the standard way to go about doing this? I used a for loop but the subscribe never hit so it would seem using a for loop is incorrect.
This is what I currently have:
for (let i = 0; i < this.selectedNodes.length; i++) {
this.fileSelectorService.fixPath(this.selectedNodes[i])
.subscribe(res => {
// This block never gets hit when using a for loop (but it does hit without the for loop)
var fixedPath = res;
})
}
}
Upvotes: 0
Views: 27
Reputation: 5602
You should use forkJoin()
after mapping the strings to an array of observable like this:
const arrayOfObs$ = this.selectedNodes.map(s => this.fileSelectorService.fixPath(s));
forkJoin(arrayOfObs$)
.subscribe(result => {
//result will be an array of the response for this.fileSelectorService.fixPath(s)
console.log(result);
//Do whatever you want to do with this result
});
Upvotes: 1