0

The task is: I have the input which implement autocomplete functionality. And i have function getItems which handle input changes and return a Promise which return an array of arbitrary objects that satisfy the filter criterion. function getItems can also return an object with method abort() which should be called on previous Promise when the previous Promise has not yet been resolved. For example:

  • we enter g... (the getItems ('g') function is called ...)
  • we press ge ... (if the previous Promise getItems ('g') has not yet been resolved - we call on the previous "promise" abort). I have no idea how to implement this. Help me please!
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Alexandr Kostrov
  • 121
  • 1
  • 1
  • 4

1 Answers1

0

Out of the box, you cannot cancel a promise once it has been created (as far as I know). Once it has been created, the process that will produce the result has been launched and can't be stopped, unless you've planned for it.

A quick Google, will bring you to numerous posts on how to get around this limitation, I personally like this one: How to cancel your promise

Aside from this, you might also be interested in reducing the amount of times your promise will be created, by using debouncing or throttling. These are means of grouping events (input-events in your case) that occur within a short timespan to create fewer events.

Stanislas
  • 1,893
  • 1
  • 11
  • 26