Permalink
3173404 Dec 26, 2017
3 contributors

Users who have contributed to this file

@pburtchaell @mikew @janeklb
36 lines (28 sloc) 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.