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

163
Vistas
Use Async/Await in event handler in react

I have a function with two arrows, which sends some form data to my api via a POST request with fetch, once the form gets submitted. I want to make fetch asynchronous, but I really have no clue where the async keyword goes in this case.

 //handles the submitting process
const handleSubmit = async (form_username, form_email, form_password) => (event) => {
  event.preventDefault();
  
    let data = {
      username: form_username,
      email: form_email,
      password: form_password,
    };

    const response = await fetch(API + "/register", {
      method: "POST",
      mode: "cors",
      headers: {
        "Content-Type": "application/json",
        "Access-Control-Allow-Origin": "*",
      },
      body: JSON.stringify(data),
    });
};

The function would then be called via handleSubmit(param1, param2,...);

However, having the async keyword infront of the first params list returns the error unexpected reserved word 'await'

Any ideas where I should place the async keyword in order to actually make it async? Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your async is declared on the wrong function: it should be the inner function that is being returned, since it is the one that contains the await keyword:

const handleSubmit = (form_username, form_email, form_password) => async (event) => {
  // Rest of the logic here
}
about 4 years ago · Juan Pablo Isaza Denunciar
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