6

I am struggling with this async await issue I have created async function and for that I am calling await but I am getting error Unexpected reserved word 'await' Below is the async function

export const addProductCall = async (product) => {
    let accessToken = window.localStorage.getItem("accessToken");
    await axios.post(SERVER_URI + '/inventoryservice/v1/item',
        product,
        { headers: { "Authorization": "Bearer " + accessToken, "Content-Type": "application/json" } })
}

Below is the function from where I am calling this function.

const handleSubmit = () => {
        const data = {
            'storeId': customerDetails.id, category, categoryToBeAdded, description,
            productCode, productName, sku, price, unit, quantity
        }
        await addProductCall(data);
    }
Andy
  • 61,948
  • 13
  • 68
  • 95
Shubham Gosewade
  • 65
  • 1
  • 1
  • 7

2 Answers2

8

You cannot use the await keyword outside of an async function :

const handleSubmit = () => {
  const data = {
    'storeId': customerDetails.id, category, categoryToBeAdded, description,
      productCode, productName, sku, price, unit, quantity
  }
  await addProductCall(data);
}

should be :

const handleSubmit = async () => {
  const data = {
    'storeId': customerDetails.id, category, categoryToBeAdded, description,
      productCode, productName, sku, price, unit, quantity
  }
  await addProductCall(data);
}
achilleb
  • 169
  • 10
6

Replace

const handleSubmit = () => {
     

with

const handleSubmit = async () => {
     

For await to work, it needs to be wrapped inside an async function

Black Mamba
  • 13,632
  • 6
  • 82
  • 105