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).
1
{
2
type: 'TYPE',
3
async payload () {
4
const fooData = await getFooData();
5
const barData = await getBarData(fooData);
6
7
return barData;
8
}
9
}
Copied!
Async/await can be combined with data for optimistic updates:
1
{
2
type: 'OPTIMISTIC_TYPE',
3
payload: {
4
data: {
5
...
6
},
7
async promise () {
8
...
9
}
10
}
11
}
Copied!
Please note there is no need to return await in an async function. See this ESLint rule for more details.
Last modified 3yr ago
Copy link