How to execute needed sources only in autocomplete


i am using autocomplete with 3 source but my requirement is i want execute third source only if first and second sources getting empty results.

is there any way to do this ?


Hi there,

You can provide a custom source.
If you look at this document,

source: function(query, callback) {
  var index = client.initIndex('myindex');, { hitsPerPage: 1, facetFilters: 'category:mycat' }).then(function(answer) {
  }, function() {

You can provide your own function as a source. In the third source, you can decide to fetch something or not based on whether the first or second source returned anything at all.

And the first and the second source should be custom ones. In the sources, you can update some global boolean flags, so that the third one can refer them.

Let me know how it goes and if you have more question.

Yes i have provided custom resource same like mention here but all resource function are calling asynchronously so i can not control them.

first Source call than second source function will call same time even results from algolia not get for first source. same things is happening for third source.

can you please share source code or any example if is it possible practically. ?


I believe this could be achievable by storing the source promises:
(pseudo code, not tested)

let source1;
let source2;
  source: () => {
    source1 = yourAsyncSource1();
    return source1;
}, { 
  source: () => {
    source2 = yourAsyncSource2();
    return source2;
}, { 
  source: () => {
    source3 = Promise.all([ source1, source2 ]);
    sources.then(([one, two]) => {
      if (one.length <= 0 && two.length <= 0) {
       return yourAsyncSource3();
      return [one, two];
   return source3;