0

I created a custom function with params using callback to reuse, but await not working

const _debounceSearch = debounce(
    async (callback: (params: RequestParams) => any, value: string | undefined, language: string | undefined) => {
      const data = await callback({ q: value, lang: language });

      console.log('data on search', data);

      return data;
    },
    300,
  );

const dataCompanies = await _debounceSearch(apiSearchCompany, searchValue, languageParam);

        console.log('dataCompanies', dataCompanies);

        if (dataCompanies) {
          setCompanySearchTags(
            data.items.map((item: Company) => ({ value: item.id, label: item.name, checked: false })),
          );
        }

I only got undefined result although i pass await prefix.

Result

Atul Sharma
  • 9,397
  • 10
  • 38
  • 65
vantrong
  • 19
  • 5

1 Answers1

0

I don't think debounce function itself can be a promise. debounce(fn) is just a regular function, but you can make a Promise whenever you want.

Maybe checkout, Using lodash debounce to return a promise

windmaomao
  • 7,120
  • 2
  • 32
  • 36
  • thanks for your answer, I finally found a way to fix it, pass option `leading: true`, then it work, thanks so much – vantrong Sep 05 '22 at 14:36
  • ok, great. i don't know what is your version of `debounce`, but here's the doc, https://lodash.com/docs/4.17.15#debounce . Seems it only takes in a function and returns a plain function. – windmaomao Sep 05 '22 at 14:38