Search code examples
reactjsasynchronouslodash

Await not working when pass async function as params LODASH


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


Solution

  • 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