When calling observer.complete()
or observer.error()
an observer stops sending data and is considered done. However, If there is a for loop in the observer, the loop will continue running even after observer.complete()
is called. Can someone explain to me this behaviour? I expect that the loop would be cut short. The current behaviour means that an interval or a while loop will forever run in an Observable
unless I unsubscribe in the code.
In the below snippet I added a console.log
to see if the log will be called after observer.complete()
.
const testObservable = new Observable( observer => {
for (let count = 0; count < 11; count++){
observer.next(count);
if (count > 5) {
observer.complete()
console.log("test")
}
if (count > 7) {
observer.error(Error("This is an error"))
}
}}
);
let firstObservable = testObservable.subscribe(
next => {console.log(next)},
error => { alert(error.message)},
() => {console.log("complete")}
)