0

I am struggling to figure out issue with async await function, I keep getting error that said

await is a reserved word

I'm new at react and the async function, I think the problem is where do I should put await in this function properly?

 handleSubmit = async (e) => {

          try {
e.preventDefault();
      this.props.form.validateFieldsAndScroll((err, values) => {
        if (!err && !this.state.added) {
          this.setState({ submit: true });
          await PopUpAdsService.create({
            name: values.name,
            photo: this.state.secureUrl,
            type: values.type,
            link: this.state.deepl,
            status: values.status,
          })

              console.log(response);
              if (response.data.status === 200) {
                Notification(
                  'success',
                  'Pop-up has been created successfully',
                );
                this.setState({ submit: false, added: true });
                setTimeout(() => {
                  this.props.history.replace('/dashboard/pop-up');
                }, 2000);
              }

          } catch (error) {
              console.log(error);
              Notification(
                'error',
                `Oops! Error occured. ${error.response.data.message}`,
              );
              this.setState({ submit: false });
            }

    async handleSubmit(e) {
      try {
    e.preventDefault();
  this.props.form.validateFieldsAndScroll((err, values) => {
        if (!err && !this.state.added) {
          this.setState({ submit: true });
          await PopUpAdsService.create({
            name: values.name,
            photo: this.state.secureUrl,
            type: values.type,
            link: this.state.deepl,
            status: values.status,
          })

          console.log(response);
          if (response.data.status === 200) {
            Notification(
              'success',
              'Pop-up has been created successfully',
            );
            this.setState({ submit: false, added: true });
            setTimeout(() => {
              this.props.history.replace('/dashboard/pop-up');
            }, 2000);
          }

      } catch (error) {
          console.log(error);
          Notification(
            'error',
            `Oops! Error occured. ${error.response.data.message}`,
          );
          this.setState({ submit: false });
        }
   }
      });

    };
Satria stuart
  • 93
  • 4
  • 12

1 Answers1

0

*updated code

If we try to use await in non-async function, that would be a syntax error. In your

this.props.form.validateFieldsAndScroll(err, values) => { ...

that is not an async function, so you have to do it like:

this.props.form.validateFieldsAndScroll(async (err, values) => { ...

More info: https://javascript.info/async-await

handleSubmit = async (e) => {
  try {
    e.preventDefault();
    this.props.form.validateFieldsAndScroll(async (err, values) => {
      if (!err && !this.state.added) {
        this.setState({ submit: true });
        await PopUpAdsService.create({
          name: values.name,
          photo: this.state.secureUrl,
          type: values.type,
          link: this.state.deepl,
          status: values.status,
        })

        console.log(response);

        if (response.data.status === 200) {
          Notification(
            'success',
            'Pop-up has been created successfully',
          );

          this.setState({ submit: false, added: true });
          setTimeout(() => {
            this.props.history.replace('/dashboard/pop-up');
          }, 2000);
        }
      }
    })
  }
  catch (error) {
    console.log(error);
    Notification(
      'error',
      `Oops! Error occured. ${error.response.data.message}`,
    );
    this.setState({ submit: false });
  }
}
Jee Mok
  • 6,157
  • 8
  • 47
  • 80