forked from HackYourFuture/Assignments
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathex4-observable.js
More file actions
25 lines (21 loc) · 979 Bytes
/
ex4-observable.js
File metadata and controls
25 lines (21 loc) · 979 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*------------------------------------------------------------------------------
Full description at: https://github.com/HackYourFuture/Assignments/tree/main/1-JavaScript/Week4#exercise-4-observable
Complete the `createObservable()` function as follows:
- The `subscribe` function should take the function passed to it as an argument
and push it onto the `subscribers` array. (Yes, you can store functions in an
array. Functions are treated in JavaScript like any other value.
- The `notify` function should iterate through, and call, all subscribers from
the `subscribers` array, passing on the notification message to each
subscriber.
------------------------------------------------------------------------------*/
export function createObservable() {
const subscribers = [];
return {
subscribe(subscriber) {
subscribers.push(subscriber);
},
notify(message) {
subscribers.forEach(subscriber => subscriber(message));
},
};
}