💫
Promise Middleware
  • Introduction
  • Getting Started
    • Introduction
    • Design Principles
  • Guides
    • Catching Errors Thrown by Rejected Promises
    • Chaining Actions
    • Comparison to other promise middleware
    • Custom Type Delimiters
    • Custom Types
    • Optimistic Updates
    • Use with Async/Await
    • Use with Reducers
    • Use with Redux Actions
    • Use with Redux Promise Actions
    • Use with Promises Resolved with Null Values
  • Upgrade Guides
    • Upgrade from 5.x to 6.0.0
    • Upgrade from 4.x to 5.0.0
    • Upgrade from 3.x to 4.0.0
    • Release History
Powered by GitBook
On this page
  1. Guides

Use with Async/Await

PreviousOptimistic UpdatesNextUse with Reducers

Last updated 7 years ago

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 :

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

Please note there is no need to return await in an async function. .

optimistic updates
See this ESLint rule for more details