为什么我的 Redux Thunk 操作 return 的结果是 Promise?
Why is the result of my Redux Thunk action return a Promise?
在我输入用户的正确电子邮件和密码后,reducer returns SIGNED_IN 值包含在 Promise 中。我很困惑为什么它不 return 只是 'SIGNED_IN':true?
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
}
}
}
你的 reducer 不应该是异步的(异步函数总是 return 一个承诺)。就像这样:
export default function (state, action)
你的操作是异步的,因为它们正在等待响应,reducer 只是一个普通函数,它接受来自异步操作的值。
在我输入用户的正确电子邮件和密码后,reducer returns SIGNED_IN 值包含在 Promise 中。我很困惑为什么它不 return 只是 'SIGNED_IN':true?
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
}
}
}
你的 reducer 不应该是异步的(异步函数总是 return 一个承诺)。就像这样:
export default function (state, action)
你的操作是异步的,因为它们正在等待响应,reducer 只是一个普通函数,它接受来自异步操作的值。