Skip to content Skip to sidebar Skip to footer

How To Fix Parsing Error: Can Not Use Keyword 'await' Outside An Async Function

Getting a error message saying Parsing error: Can not use keyword 'await' outside an async function from below code for Redux action. What is the proper way to write this? export a

Solution 1:

You need to have async keyword for the inner function

exportfunctiongetData() {
  returnasync (dispatch) => {
    try {
      const data = awaitAPI.graphql(graphqlOperation(query))
      dispatch({ type: "DATA_AVAILABLE", data: data.data.listData.items });
    } catch(err) {
      console.log('error: ', err)
    }
  }
}

Solution 2:

You can pull your await function out of your return function as well and pass dispatch as a param to getData function.

exportasyncfunctiongetData(dispatch) {
  const data = awaitAPI.graphql(graphqlOperation(query))
  dispatch({ type: "DATA_AVAILABLE", data: data.data.listData.items });
}

then you call it:

var dispatchFunc = function (params) {console.log(params);}
getData(dispatchFunc).then(function() {}).catch(function(err) { ... })

Post a Comment for "How To Fix Parsing Error: Can Not Use Keyword 'await' Outside An Async Function"