我是全新的React和Redux开发者。
我正在使用react-redux调用AWS Cognito服务,该服务需要一个包含成功和失败回调函数的对象。当我在成功回调函数内部console.log时,我从AWS Cognito获取我的JWT;但是,由于它不是生成器函数(function*
),我如何在此回调函数内部使用 yield put()
?
以下是一些代码:
export function* getAWSToken(){
// username, password and userPool come from react state
// not showing code for brevity.
const userData = {
Username: username,
Pool: userPool,
};
const authenticationData = {
Username : username,
Password : password,
};
const cognitoUser = new AWSCognito.CognitoIdentityServiceProvider.CognitoUser(userData);
const authenticationDetails = new AWSCognito.CognitoIdentityServiceProvider.AuthenticationDetails(authenticationData);
// This here is the callback
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess(result){
yield put(cognitoTokenObtained(result.getIdToken().getJwtToken())
},
onFailure(err){}
});
}
getAWSToken
,cognito 都会给你一个新的令牌,那么使用生成器是否会改善情况? - David GilbertsongetAWSToken(yourCallback)
,然后在onSuccess
中调用该函数,如callback(result)
。但是,如果有使用生成器函数的redux-saga
特定原因,那么我不想建议破坏redux-saga
的东西。 - David Gilbertsonyield
?你有任何错误吗?因为回调函数不是生成器,但是你的getAWSToken
是,所以在这里使用yield
不应该有任何问题。 - Pierre Criulanscy