Weird Javascript Syntax Return {unsubscribe() {}};
Solution 1:
You say that the code return {unsubscribe() {}};
is a "Function call inside code block followed by empty object"; that is incorrect.
What is actually happening is that the function, sequenceSubscriber
, is returning an Object
with a function named "unsubscribe" as a property. That function does nothing. This is utilizing the new function shorthand you can see here: Method definitions
Consider the code:
const foo = {
bar(){}
};
creates an Object foo
that has a function
, bar
, that does nothing.
The reason why it is doing this is to fulfill the interface contract that Rx defines for Observables
as well as the Subscription
interface defined in the tc39/proposal-observable:
interface Subscription {
// Cancels the subscriptionunsubscribe() : void;
// A boolean value indicating whether the subscription is closed
get closed() : Boolean;
}
Solution 2:
The function returns an object that contains an unsubscribe
function that does nothing. As the comment says the unsubscribe
function is not needed because the observer emits synchronously.
Post a Comment for "Weird Javascript Syntax Return {unsubscribe() {}};"