Skip to content Skip to sidebar Skip to footer

Async Type Ahead Input Buffer Or Queue

I'm working on a type ahead input component and part of my implementation is a queue or buffer FIFO of cancelable promises. Because the typing can happen much faster than the async

Solution 1:

As your code immediately cancels any in-flight request before sending a new one, your queue will never contain more than one element. So it's useless to use a queue, a simple currentRequest field is enough to ensure you can handle only the most recent request, no matter in what order they are processed.

A common practice in type-ahead controls is to throttle the input events, i.e. wait a short amount of time for another change before actually sending an AJAX request. This avoids sending too many requests when users type several letters in a quick fashion.

Both of those problems can be abstracted from your code if you are willing to use reactive programming techniques, e.g. using RxJS http://reactivex.io/.

In fact somewhere down on the following RxJS page you will find an example that does exactly that: observing input changes, requiring at least 2 chars, debouncing, querying a webservice and then handling the results: https://github.com/Reactive-Extensions/RxJS

Post a Comment for "Async Type Ahead Input Buffer Or Queue"