Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda