Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
problem using set timeout and fetch together

I'm trying fetch data every time user types something(using useEffect for that), also I want to delay fetch function using setTimeout, so when user stops typing, it should wait 3 second and then fetch github profiles, but when i type let's say "giorgi", after 3 seconds instead of getting only "giorgi" profiles, it waits 3 seconds goes through all of the past fetches, first rapidly renders profiles containing gi, after that gio, then gior and etc and finally only giorgi profiles. I think I'm doing something wrong in settimeout function or I'm not using it right.

useEffect(() => {
    const getSearchedProfile = async (searchValue) => {
        const api_url = 'https://api.github.com/search/users?q='+searchValue+'+in:login&per_page=20';
        const fetchProfile = await fetch(api_url);
        const profile = await fetchProfile.json();
            setProfiles(profile.items);
    }

    const getPoPularProfile = async () => {
        const api_url = 'https://api.github.com/search/users?q=repos:followers:%3E35000&language:javascript&page=1&per_page=10';
        const fetchProfile = await fetch(api_url);
        const profile = await fetchProfile.json();
            setProfiles(profile.items);
    }

    if(searchValue !== ''){
      setTimeout(() => {
        getSearchedProfile(searchValue);
        setShow(true);
      }, 3000);
    }

    if(searchValue === ''){
      setTimeout(() => {
        getPoPularProfile();
        setShow(false);
      }, 3000);
    }
}, [searchValue])
about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda