ECMA6 generators: yield promise

后端 未结 2 432
不知归路
不知归路 2020-12-09 19:30

As I understand it ECMA6 generators are supposed to be able to yield to a function that returns a promise, eventually returning the resolved/rejected. Making the code read m

2条回答
  •  醉酒成梦
    2020-12-09 20:05

    tl;dr: The promise yielded by the generator has to move the generator forward.


    If you look at first examples in http://davidwalsh.name/async-generators, you will notice that the async function actually moves the iterator forward:

    function request(url) {
        // this is where we're hiding the asynchronicity,
        // away from the main code of our generator
        // `it.next(..)` is the generator's iterator-resume
        // call
        makeAjaxCall( url, function(response){
            it.next( response );               // <--- this is where the magic happens
        } );
        // Note: nothing returned here!
    }
    

    But since you are working with promises, we can improve on that, a little bit. y.next().value returns a promise, and you'd have to listen to that promise. So instead of writing console.log(y.next()), you'd write:

    var promise = y.next().value;
    promise.then(y.next.bind(y)); // or promise.then(function(v) { y.next(v); });
    

    Babel demo

    Of course this not very practical, because now you cannot access the next yielded value and you don't know when the generator will be done. However, you could write a recursive function which takes care of that. That's what runGenerator introduced later in this article takes care of.

提交回复
热议问题