【发布时间】:2016-08-01 21:41:19
【问题描述】:
我是 React 和 Redux 的新手。
我正在使用 react-redux 调用 AWS Cognito 服务,该服务接受一个包含成功和失败回调的对象。当我在成功回调中使用 console.log 时,我从 AWS Cognito 取回了我的 JWT;但是,我怎么能在这个回调中yield put(),因为它不是生成器函数(function*)。
这里有一些代码:
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 都会给你一个新的令牌,那么生成器会改善事情吗? -
如前所述,我是 React 和 Redux 的超级新手,所以我正在关注示例。似乎无论在哪里使用 redux-saga,都会使用生成器函数。如果不是这种情况,请告知。
-
嗯,这对我来说似乎过于复杂。您可以只使用普通函数,将回调传递给
getAWSToken(yourCallback),然后在onSuccess中调用该函数,如callback(result)。但是,如果有一些redux-saga特定的原因使用生成器函数,那么我不想建议破坏redux-saga的东西。 -
你为什么说你不能在你的回调中使用
yield?你有什么错误吗?因为回调不是生成器,但你的getAWSToken是,所以你不应该对这里的yield有任何问题。
标签: reactjs redux yield amazon-cognito redux-saga