I am working on a search functionality for my project. Once the user types anything on the search bar; on any change in search text I'll be sending the text to backend for validation and receive a response (error or no error in text):
this.searchBar.on('change', () => {
http.post('api_link', {searchText:
this.serachBar.searchText}).subscribe(resp => {
this.resp = resp['Result'];
});
})
Now as the user continuously types in the search bar, multiple validation responses are incoming through back-end. However, on any new change only the latest subscription is valid and any previous call to api is useless.
Is there any way I can cancel the previous subscription on any new call to api using subscribe?
Note: It might seem possible to just wait for all responses but I am also going to display the responses below the search bar(showing a loader till then). So, rather than transition between various response states I want loader to keep loading until the latest response is available.