为什么我的Redux Thunk行动的结果是一个承诺?
输入用户正确的电子邮件和密码后,reducer将返回封装在承诺中的签名值。我不明白为什么它不简单地返回“SIGNED_IN”:真的吗为什么我的Redux Thunk行动的结果是一个承诺?,redux,redux-thunk,Redux,Redux Thunk,输入用户正确的电子邮件和密码后,reducer将返回封装在承诺中的签名值。我不明白为什么它不简单地返回“SIGNED_IN”:真的吗 authentication: Promise __proto__: Promise [[PromiseStatus]]: "resolved" [[PromiseValue]]: Object SIGNED_IN: true 我将派遣此行动: const mapDispatchToProps = dispatch => ({ authUser: ()
authentication: Promise
__proto__: Promise
[[PromiseStatus]]: "resolved"
[[PromiseValue]]: Object
SIGNED_IN: true
我将派遣此行动:
const mapDispatchToProps = dispatch => ({
authUser: () => dispatch(authUser('richard@tiger.com', 'xzy98765'))
})
操作创建者:
export const authUser = (email, password) => async dispatch => {
console.log('asd')
try {
const user = await Auth.signIn(email, password)
if (user.challengeName === 'MFA_SETUP') {
// This happens when the MFA method is TOTP
// The user needs to setup the TOTP before using it
// More info please check the Enabling MFA part
//Auth.setupTOTP(user);
} else {
// The user directly signs in
console.log(user)
return dispatch({type:'SIGN_IN_USER_SUCCESS'})
}
} catch (e) {
console.log(e)
return dispatch({type:'SIGN_IN_USER_FAIL'})
}
}
减速器:
export default async (state, action) => {
console.log('authReducer state',state)
switch (action.type) {
case 'SIGN_IN_USER_SUCCESS':
return {
...state,
SIGNED_IN: true
}
case 'SIGN_IN_USER_FAIL':
return {
...state,
SIGNED_IN: false
}
}
}
减速器不应该是异步的(异步函数总是返回一个承诺)。就这样做吧:
export default function (state, action)
您的操作是异步的,因为它们正在等待响应,减速机只是一个接受异步操作值的普通函数。您的减速机不应该是异步的(异步函数总是返回承诺)。就这样做吧:
export default function (state, action)
您的操作是异步的,因为它们正在等待响应,reducer只是一个普通函数,它接受来自异步操作的值