async autocomplete debounce

What is the correct way of debouncing an async completion. I tried debouncing the source function but the tooltip won’t load until the very next input after the debounce threshold.

import { debounce } from "lodash";

const state = EditorState.create({
    doc: "",
    extensions: [
      basicSetup,
      oneDark,
      getLanguage(language),
      autocompletion({
        override: [
          debounce(
            async function sources(context: CompletionContext) {
              const word = context.matchBefore(/.*/); 

              if (word.from == word.to && !context.explicit) return null;

              const options = await fetchStuff(word.text);

              return { from: word.from, options };
            },
            300
          )
        ],
      }),
    ],
  });

The autocomplete extension does the debouncing, your source function shouldn’t.

It doesn’t seem to be working. If I remove the debounce function each keystroke will call the completionSource function.

Based on this thread I ended up creating a facet to store debounced callbacks and manually calling the startCompletion command with a EditorView.updateListener extension.