Is there a way to make observables wait until the previous one has completed when they are created from an array? Any help is appreciated!
export class AppComponent{
arr: number[] = [5, 4, 1, 2, 3];
fetchWithObs() {
from(this.arr)
.pipe(
map((value) => {
return this.getData(value);
})
)
.subscribe((data) => {
console.log(data);
/**
* The expected output should be
* 5 completed
* 4 completed
* 1 completed
* 2 completed
* 3 completed
*/
});
}
getData(p: number) {
return new Observable<string>((s) => {
setTimeout(() => {
s.next(`${p} completed`);
s.complete();
}, p * 1000);
});
}
}