Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to subscribe to the observable and log the data.
Angular
this.dataService.getData().[1](data => console.log(data)); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'map' instead of 'subscribe' which only transforms data but does not listen.
Using 'pipe' without subscribing does not trigger the observable.
✗ Incorrect
The subscribe method is used to listen to the observable and receive data.
2fill in blank
mediumComplete the code to handle errors when subscribing to the observable.
Angular
this.apiService.fetchItems().subscribe({ next: items => this.items = items, error: [1] }); Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a non-function value to error handler.
Confusing error handler with next handler.
✗ Incorrect
The error property expects a function to handle errors. Here, it logs the error.
3fill in blank
hardFix the error in the subscription to properly unsubscribe on component destroy.
Angular
subscription = this.service.getUpdates().[1](data => this.update(data));
ngOnDestroy() {
this.subscription.unsubscribe();
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pipe' which returns an observable, not a subscription.
Trying to unsubscribe something that is not a subscription.
✗ Incorrect
Only subscribe returns a subscription object that can be unsubscribed.
4fill in blank
hardFill both blanks to subscribe to an observable and handle completion.
Angular
this.observable.[1]({ next: val => console.log(val), [2]: () => console.log('Complete') });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pipe' instead of 'subscribe'.
Confusing 'error' and 'complete' handlers.
✗ Incorrect
The subscribe method accepts an object with next, error, and complete handlers.
5fill in blank
hardFill all three blanks to subscribe with next, error, and complete handlers.
Angular
this.dataStream.[1]({ [2]: data => this.process(data), [3]: err => this.handleError(err), complete: () => this.finish() });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pipe' instead of 'subscribe'.
Mixing up 'next' and 'error' keys.
✗ Incorrect
Use subscribe with an object containing next, error, and complete callbacks.