CHALLENGE
const source = {
value: 1,
subscribers: new Set(),
subscribe(fn) {
this.subscribers.add(fn);
return () => this.subscribers.delete(fn);
},
next(newValue) {
this.value = newValue;
this.subscribers.forEach(fn => fn(this.value));
}
};
const mapped = {
value: undefined,
source,
transform: x => x * 2,
init() {
this.source.subscribe(val => {
this.value = this.transform(val);
console.log(`Mapped: ${this.value}`);
});
}
};
mapped.init();
source.next(3);
source.next(5);
console.log(`Final: ${mapped.value}`);
source.next(2);What is the output?
Anonymous Quiz
19%
Mapped: 6 Mapped: 10 Mapped: 4 Final: 10
43%
Final: 10 Mapped: 4
37%
Mapped: 6 Mapped: 10 Final: 10 Mapped: 4
2%
Mapped: 6 Final: 10 Mapped: 10 Mapped: 4
❤1👍1🔥1