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

208
Visualizações
Add additional params to a function being passsed in

I am using the useAsync hook, as described in https://usehooks.com/useAsync/ The existing code works well, something like this:

const useAsync = (asyncFunction, immediate = true) => {
    const execute = useCallback(() => {
      return asyncFunction()
        .then((response) => {
          console.log('SUCCESS', resposne)
        })
    }, [asyncFunction]);

    return { execute};
}

const Component = () => {
    const getFile = async () => {
        const payload = { fileId };
        return getFileInfo(payload);
    };

    const { execute  } = useAsync(getFile, false);
    useEffect(() => {execute()}, []);
}

But, I need to modify useAsync such that it adds a Token as an additional parameter to the asyncFunction it is given. Something like:

const useAsync = (asyncFunction, immediate = true) => {
    const execute = useCallback(() => {

        // GET THE TOKEN
        getAccessToken().then((token) => { 
            return asyncFunction()  // ADD token AS A PARAM HERE SOMEHOW?
              .then((response) => {
                console.log('SUCCESS', resposne)
              })
        }

    }, [asyncFunction]);

    return { execute};
}

I suspect that I might need to bind() (MDN-Bind) the token to the given asyncFunction as an extra parameter but I can't make it work. Any suggestions?

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