Add separate project for async-thunk implementation
See original GitHub issue@aikoven provided a solution for redux-thunk integration and I think this should be a separate package with the util + documentation. I use redux-thunk, so I can contribute to the usage documentation.
I have modified it slightly for readability and added TError
generic.
import { AsyncActionCreators } from 'typescript-fsa'
// https://github.com/aikoven/typescript-fsa/issues/5#issuecomment-255347353
function wrapAsyncWorker<TParameters, TSuccess, TError>(
asyncAction: AsyncActionCreators<TParameters, TSuccess, TError>,
worker: (params: TParameters) => Promise<TSuccess>,
) {
return function wrappedWorker(dispatch, params: TParameters): Promise<TSuccess> {
dispatch(asyncAction.started(params));
return worker(params).then(result => {
dispatch(asyncAction.done({ params, result }));
return result;
}, (error: TError) => {
dispatch(asyncAction.failed({ params, error }));
throw error;
});
}
}
export default wrapAsyncWorker
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:10 (3 by maintainers)
Top Results From Across the Web
Setup a Redux Application using createAsyncThunk and React
Now let's set it up from scratch. Step 1: create a react project via the following: npx create-react-app reduxtoolkitapp. Step 2: Afterward, ...
Read more >createAsyncThunk - Redux Toolkit
First, create the thunk const fetchUserById = createAsyncThunk( 'users/fetchByIdStatus', async (userId: number, thunkAPI) => {
Read more >Using Redux Toolkit's createAsyncThunk - LogRocket Blog
Learn how to use the createAsyncThunk API to perform asynchronous tasks in Redux apps and handle common errors.
Read more >Redux Toolkit Tutorial - 24 - Async Thunks - YouTube
Courses - https://learn.codevolution.dev/ Support UPI - https://support.codevolution.dev/ Support Paypal ...
Read more >10#. How to use createAsyncThunk in redux toolkit - YouTube
React.js Real-World Projects. Lama Dev. Lama Dev ... NestJs Course for Beginners - Create a REST API. freeCodeCamp.org. freeCodeCamp.org.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I was thinking about making wrapAsyncWorker act like how I declare thunks already, so I wrote this:
You use it like this (for example):
And then somewhere later:
@ttamminen @aikoven https://github.com/xdave/typescript-fsa-redux-thunk
npm install --save typescript-fsa-redux-thunk
oryarn add typescript-fsa-redux-thunk