- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我是 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){}
});
}
最佳答案
如果你正在使用 redux-saga (这很棒),你可以使用 call effect将像 cognitoUser.authenticateUser 这样的异步回调转换为一组由中间位置执行的指令。
当中间件解析调用时,它将逐步通过生成器到下一个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);
// Note: since you're invoking an object method
// you'll want to pass the object's context (this) with the call via an array
const token = yield apply(cognitoUser, cognitoUser.authenticateUser, [authenticationDetails, { onSuccess(response){return response}, onFailure(){} }]);
yield put(cognitoTokenObtained(token.getIdToken().getJwtToken());
}
还有this incredible tutorial我强烈推荐。
编辑:为了简洁起见,您省略了一些代码,但我强烈建议您将代码包装在 try catch 中的生成器内,因为您依赖于来自 API 的外部 IO。
关于reactjs - 在成功回调中调用yield put(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38708583/
function* generatorFunction() { yield (yield 1)(yield 2)(yield 3)(); } var iterator = generatorFun
ECMAScript 6 应该带来生成器函数和迭代器。生成器函数(具有 function* 语法)返回一个迭代器。迭代器有一个 next 方法,当重复调用时,该方法会执行生成器函数的主体,并在每个 y
ECMAScript 6 应该引入生成器函数和迭代器。生成器函数(具有 function* 语法)返回迭代器。迭代器有一个 next 方法,当重复调用时,它会执行生成器函数的主体,在每个 yield
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 2 年前。 Improve t
自 python 2.5 以来,可以将 send()、throw()、close() 放入生成器中。在定义的生成器中,可以通过执行以下操作来“捕获”发送的数据: def gen(): whil
return的区别和 yield似乎很清楚,直到我发现还有 yield from以及将两者结合起来的可能性 return和 yield在完全相同的功能! 我对return的理解之后的一切都是 不是 执
假设我有这个部分,我正在尝试渲染 #layouts/_subheader.html.erb 当我在这样的 View 中使用这个部分时 Content For Yield
yield操作符是由编译器在底层实现的,该编译器生成一个实现符合 IEnumerable 的状态机的类。和 IEnumerator . 给定一个罗斯林 MethodDeclarationSyntax
$item) echo "$index $item" . PHP_EOL; } resolve(generator1()); echo PHP_EOL; resolve(gener
这个问题在这里已经有了答案: Why converting list to set is faster than converting generator to set? (1 个回答) List c
是否有一个单行代码来获取生成器并生成该生成器中的所有元素?例如: def Yearly(year): yield YEARLY_HEADER for month in range(1, 13)
刚发现yield from 结构,在我看来这有点像反向的yield,而不是从生成器中获取对象,您插入/将对象发送到生成器。喜欢: def foo(): while True:
考虑以下代码: def mygen(): yield (yield 1) a = mygen() print(next(a)) print(next(a)) 输出产量: 1 None 解释器
Guido van Rossum,在 2014 年关于 Tulip/Asyncio 的演讲中 shows the slide : Tasks vs coroutines Compare: res =
谁能帮我理解“yield self”和“yield”的区别? class YieldFirstLast attr_accessor :first, :last def initiali
这是我目前使用 Laravel 5 实现的 Open Graph 标签: app.blade.php @yield('title') page.blade.php @extends('app'
在 Tornado 中,我们通常会编写如下代码来异步调用函数: class MainHandler(tornado.web.RequestHandler): @tornado.gen.coro
本文整理了Java中aQute.bnd.indexer.analyzers.Yield.yield()方法的一些代码示例,展示了Yield.yield()的具体用法。这些代码示例主要来源于Github
我们有超过 100 个共同基金的每日返回,我们希望将这些返回转换为月度返回。每月返回不应是每个月的平均值,而是每个月末的资金返回。基金在不同的时间点开始和结束,它们需要自己保留(不是每个月的共同基金
如何实现 C# yield return使用 Scala 延续?我希望能够编写 Scala Iterator s 风格相同。在 this Scala news post 的评论中有刺伤,但它不起作用(
我是一名优秀的程序员,十分优秀!