javascript 通过 yield & co 获得承诺的价值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21846390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting a promise's value via yield & co
提问by Mark Kahn
I'm trying to figure out how to get the value of a promise via yield
, possibly with "co":
我试图弄清楚如何通过 获得承诺的价值yield
,可能是“co”:
function *(){
var someVar = yield functionThatReturnsAPromise();
}
The called function is not a generator, just a normal function. With the above, someVar == Promise
, but I want the resolved value. Does coor some other library have a way of doing this?
被调用的函数不是生成器,只是一个普通的函数。有了上述,someVar == Promise
,但我想要已解决的值。是否合作或其他一些库有这样做的方法吗?
采纳答案by Klaster_1
Yes, cocan do that. You'll have to wrap parent function inside co
call:
是的,co可以做到这一点。您必须将父函数包装在co
调用中:
co(function *(){
var someVar = yield functionThatReturnsAPromise();
})()
someVar
inside will become resolved value. If promise gets rejected, error can be cought with basic try {} catch (e) {}
statements.
someVar
里面会变成解析值。如果 promise 被拒绝,则可以使用基本try {} catch (e) {}
语句来排除错误。
回答by Lodewijk Bogaards
Typically a yield acts returns the same value to its own paused execution (left hand side of the yield function) as to the calling function of the generator. In this simple example counting from 1 to 5 example the input of the yield is the output of the yield to the generator function as well as to the generator's execution path:
通常,yield 行为返回与生成器的调用函数相同的值到它自己的暂停执行(yield 函数的左侧)。在这个从 1 到 5 计数的简单示例中,yield 的输入是生成器函数以及生成器执行路径的 yield 输出:
function* inc() {
var g = 0;
while (true) {
g = yield g + 1;
}
}
var incGen = inc();
for (i = incGen.next().value; i <= 5; i = incGen.next(i).value) {
console.log(i); // ^ input of generator is last output
}
However, the calling function may also call the generator, but replace the output the last yield with another value or even throw an exception to the generator's execution. In the case of promise a function that returns a promise, may yield the result of that promise instead of the promise itself. So in this case:
然而,调用函数也可能调用生成器,但将最后一个 yield 的输出替换为另一个值,甚至抛出异常到生成器的执行。在promise 的情况下,返回promise 的函数可能会产生该promise 的结果而不是promise 本身。所以在这种情况下:
var someVar = yield functionThatReturnsAPromise();
^ output != ^ input
you want the yield to act as a function that takes a promise as an input and returns a resolved promise as an output to the generator function.
您希望 yield 充当一个函数,该函数将 Promise 作为输入并将已解析的 Promise 作为输出返回给生成器函数。
It so happens co
is able to do exactly this for you. All you need to do is feed your generator function to the co
function:
它恰好co
能够为您做到这一点。您需要做的就是将生成器函数提供给co
函数:
co(function *(){
var someVar = yield functionThatReturnsAPromise();
})
To better understand how this works, here is an example of a function, that does the same thing as co:
为了更好地理解它是如何工作的,这里有一个函数的例子,它和 co 做同样的事情:
function async(makeGenerator){
return function (){
var generator = makeGenerator.apply(this, arguments)
function handle(result){
if (result.done) return result.value
return result.value.then(function (res){
return handle(generator.next(res)) // <- sets output of yield to the promise result
}, function (err){ // and returns input promise
return handle(generator.throw(err)) // <- throw promise exception to generator function
})
}
return handle(generator.next()) // <- first time call, input of yield is a promise
}
}
source is from Forbes Lindesay's now famous presentation about this concept