Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Await is a reserved word error inside async function

People also ask

Does await only works inside an async function?

As said, await only works inside async function. And await just waits for the promise to settle within the async function.

What is await in async?

The await operator is used to wait for a Promise . It can only be used inside an async function within regular JavaScript code; however it can be used on its own with JavaScript modules.

What is the difference between await and async?

In using async and await, async is prepended when returning a promise, await is prepended when calling a promise. try and catch are also used to get the rejection value of an async function.

Can async await be used inside a promise?

Inside an async function you can use the await keyword before a call to a function that returns a promise. This makes the code wait at that point until the promise is settled, at which point the fulfilled value of the promise is treated as a return value, or the rejected value is thrown.


In order to use await, the function directly enclosing it needs to be async. According to your comment, adding async to the inner function fixes your issue, so I'll post that here:

export const sendVerificationEmail = async () =>
  async (dispatch) => {
    try {
      dispatch({ type: EMAIL_FETCHING, payload: true });
      await Auth.sendEmailVerification();
      dispatch({ type: EMAIL_FETCHING, payload: false }))
    } catch (error) {
      dispatch({ type: EMAIL_FETCHING, payload: false });
      throw new Error(error);
    }
  };

Possibly, you could remove the async from the outer function because it does not contain any asynchronous operations, but that would depend on whether the caller of that sendVerificationEmail is expecting sendVerificationEmail to return a promise or not.