Getting a promise's value via yield & co

前端 未结 2 1466
隐瞒了意图╮
隐瞒了意图╮ 2020-12-17 18:25

I\'m trying to figure out how to get the value of a promise via yield, possibly with \"co\":

function *(){
    var someVar = yield functionThatR         


        
2条回答
  •  被撕碎了的回忆
    2020-12-17 19:03

    Yes, co can do that. You'll have to wrap parent function inside co call:

    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.

提交回复
热议问题