Why doesn't this result in an infinite loop? #6071
-
Can someone explain why this isn't an infinite loop? Seems to be something about execution order with This code has a subject calling const subject$ = new Subject();
subject$
.pipe(
startWith(null),
switchMap(() => (
throwError('error')
.pipe(
catchError(() => {
subject$
.next()
return of(null);
}),
)
)),
)
.subscribe() There are three fixes to make this infinitely loop:
startWith(null),
delay(0),
merge(
of(null),
subject$,
)
catchError(() => {
if (isFirstTime) {
isFirstTime = false
setTimeout(() => {
subject$
.next()
})
}
else {
subject$
.next()
}
return of(null);
}), Looks like this might be related to an issue I had a few years back when trying to do the same thing; calling a subject's |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 9 replies
-
My guess is that |
Beta Was this translation helpful? Give feedback.
-
are we just talking about behavior or is there a use case to this? |
Beta Was this translation helpful? Give feedback.
My guess is that
startWith
emits before it subscribes to its source, so it's not subscribed at the time thenext
call is made. Feature. Not a bug.