0

I created thunk by redux toolkit "createAsyncThunk" and i do some async operations here

export let Login: any = createAsyncThunk("authPage/Login",
    async ({values, action}: any, {dispatch, rejectWithValue}) => {
        try {

            let response = await AuthAPI.Login(values)

            if (response.data.resultCode === 0) {

                let responseFromAuthMe = await HeaderAPI.AuthMe()
                if (responseFromAuthMe.data.resultCode === 0) {
 -  -  -  -  -  - dispatch(HeaderLogin()) -  -  -  -  -  -  -  -  -
                }
            } else {
                if (response.data.resultCode === 10) {
                    let response = AuthAPI.GetCaptcha()
                    dispatch(getCaptcha(response))


                }
                action.setStatus({error: response.data.messages})
            }
        } catch (err: any) {
            return rejectWithValue(err.response.data)
        }
    })

but one moment i need use another thunk like "HeaderLogin" :

export let HeaderLogin: any = createAsyncThunk("authPage/HeaderLogin",
    async ({}, {dispatch}) => {

        let responseFromAuthMe = await HeaderAPI.AuthMe()
        if (responseFromAuthMe.data.resultCode === 0) {
            let {id, email, login} = responseFromAuthMe.data.data;
            //TODO check 4 arg - isLogin
            dispatch(setAuthUserData({id, email, login}))

            let responseLogin = await HeaderAPI.Login(login)

            return responseLogin.data.items.filter((u: any) => {
                if (id === u.id) {
                    return u
                }
            })
        }
    })

but thunk is not called. maybe have any way that calls thunk?

How i can use thunk inside another thunk in Redux?

Yefimchuk
  • 67
  • 2
  • 6
  • 2
    You can dispatch a thunk from inside another thunk, since you have access to dispatch. Explain what you mean with *it doesn't work*. – Cesare Polonara Apr 06 '22 at 17:00
  • i also thought when i dispatch thunk inside another thunk it's will be work, but no, he is not called – Yefimchuk Apr 07 '22 at 12:32

1 Answers1

0

his problem is solved, you need to throw an empty object, if you don't even have any arguments, then he is called

Yefimchuk
  • 67
  • 2
  • 6