本文介绍了通过收益率获得承诺的价值合作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正试图弄清楚如何通过 yield
获得承诺的价值,可能还有co:
I'm trying to figure out how to get the value of a promise via yield
, possibly with "co":
function *(){
var someVar = yield functionThatReturnsAPromise();
}
被调用的函数不是生成器,只是普通函数。使用上面的 someVar == Promise
,但我想要解析的值。 co 或其他一些图书馆是否有办法做到这一点?
The called function is not a generator, just a normal function. With the above, someVar == Promise
, but I want the resolved value. Does co or some other library have a way of doing this?
推荐答案
是的, co 可以做到这一点。你必须在 co 中包含父函数:
Yes, co can do that. You'll have to wrap parent function inside co
call:
co(function *(){
var someVar = yield functionThatReturnsAPromise();
})()
someVar
里面将成为解析值。如果承诺被拒绝,可以使用基本的 try {} catch(e){}
语句来解决错误。
someVar
inside will become resolved value. If promise gets rejected, error can be cought with basic try {} catch (e) {}
statements.
这篇关于通过收益率获得承诺的价值合作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!