Skip to content

Latest commit

 

History

History
35 lines (28 loc) · 777 Bytes

async-await.md

File metadata and controls

35 lines (28 loc) · 777 Bytes

Use with Async/Await

Instead of chaining your async code with .then().then().then(), you can use async/await.

Consider this example. First, request fooData, then request barData and exit the function (also resolving the promise).

{
  type: 'TYPE',
  async payload () {
    const fooData = await getFooData();
    const barData = await getBarData(fooData);

    return barData;
  }
}

Async/await can be combined with data for optimistic updates:

{
  type: 'OPTIMISTIC_TYPE',
  payload: {
    data: {
      ...
    },
    async promise () {
      ...
    }
  }
}

Please note there is no need to return await in an async function. See this ESLint rule for more details.