· 6 years ago · Oct 12, 2019, 06:46 PM
1keyPressSources.pipe(
2 // Input handlers to take input value for searching
3 switchMap(value => {
4 return from(fetchCakesByName(value)).pipe(
5 // Only make an API request for searching after 0.5s since user stopped pressing a key
6 debounce(0.5),
7 // Retry 3 times if an API request fails
8 retry(3),
9 // Cancel the search when press Esc or clear input
10 takeUntil(value => value === 'Esc' || value === ''),
11 // Handling errors by showing an error alerts
12 catchError(error => alert('Error in getting cakes'))
13 )
14 }),
15)