This causes the following error: Cannot read property 'length' of undefined
const msg$ = new Subject<string>();
msg$.subscribe(console.log)
of("Hello").subscribe(msg$.next);
If, however, I wrap msg$.next in a function, then it works without any errors.
- Lambda function
const msg$ = new Subject<string>();
msg$.subscribe(console.log)
of("Hello").subscribe(greeting => msg$.next(greeting));
- Anonymous function
const msg$ = new Subject<string>();
msg$.subscribe(console.log)
of("Hello").subscribe(function(greeting){
msg$.next(greeting);
});
- Named function
function nextMsg(greeting){
msg$.next(greeting);
}
const msg$ = new Subject<string>();
msg$.subscribe(console.log)
of("Hello").subscribe(nextMsg);
They're all just wrapper functions that seemingly do nothing but call the next function. What's going on here? Seems like there's a JavaScript gotcha I'm not aware of at work here.