javascript中双向生成器的真实使用情况

Real world usage of two way generators in javascript

本文关键字:真实 用情 情况 javascript      更新时间:2023-09-26

来自C#世界,我很想了解javascript中双向生成器的一些实际用法。我可以理解发电机在一般情况下是如何有用的,但对于双向发电机来说就不一样了。我们可以将它与RxJS或类似功能一起使用吗?你能解释一下可以使用这种方法的模式/场景吗?

function* interrogate() {
   let name = yield "What is your name?";
   let color = yield "What is your favorite color?";
   return `${name}'s favorite color is ${color}.`;
}
let it = interrogate();
it.next();         // { value: "What is your name?", done: false }
it.next('Ethan');  // { value: "What is your favorite color?", done: false }
it.next('orange'); // { value: "Ethan's favorite color is orange.", done:true }

David Walsh有一个关于ES6发电机的博客

他有一个的例子

function *foo(x) {
    var y = 2 * (yield (x + 1));
    var z = yield (y / 3);
    return (x + y + z);
}
var it = foo( 5 );
// note: not sending anything into `next()` here
console.log( it.next() );       // { value:6, done:false }
console.log( it.next( 12 ) );   // { value:8, done:false }
console.log( it.next( 13 ) );   // { value:42, done:true }

但是,这个用例和您的示例一样是经过制造的。

在他的总结中,他说

人们很自然地想知道这种新的异国玩具会做什么实际上是为了您的代码。不过,他们还有很多。we have的常用口语形式只是触及了表面。所以我们必须先潜得更深发现他们可以/将要有多强大。

  • 错误处理是如何工作的
  • 一个生成器可以调用另一个生成器吗
  • 异步编码如何与生成器一起工作

这些问题以及更多内容将在后续文章中介绍来了,请继续关注!

在稍后的博客中,他有以下片段(以及其他一些)

// run (async) a generator to completion
// Note: simplified approach: no error handling here
function runGenerator(g) {
    var it = g(), ret;
    // asynchronously iterate over generator
    (function iterate(val){
        ret = it.next( val );
        if (!ret.done) {
            // poor man's "is it a promise?" test
            if ("then" in ret.value) {
                // wait on the promise
                ret.value.then( iterate );
            }
            // immediate value: just send right back in
            else {
                // avoid synchronous recursion
                setTimeout( function(){
                    iterate( ret.value );
                }, 0 );
            }
        }
    })();
}
runGenerator( function *main(){
    var result1 = yield request( "http://some.url.1" );
    var data = JSON.parse( result1 );
    var result2 = yield request( "http://some.url.2?id=" + data.id );
    var resp = JSON.parse( result2 );
    console.log( "The value you asked for: " + resp.value );
} );

这似乎更真实一些。

他总结了

简单地说:生成器+产生的承诺将两者的优点结合在一起获得真正强大和优雅的同步(看起来)异步流的世界控制表达能力。使用简单的包装器实用程序(许多库已经提供了),我们可以自动运行生成器完成,包括sane和sync(查找)错误处理!