Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

161
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!