js return value generator

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/mynewdays/article/details/93847518
 function* cars(action) {
        const imposter = yield('hello ' + action);
        yield imposter;
    }
    const c1=cars();
    const res1=c1.next();
    console.log(res1);

    const res2=c1.next('本田');
    console.log(res2);

Next after the first execution method executes a first generator

const imposter = yield('hello ' + action);

What is the return value of yield? Under normal circumstances is undefiend, action and parameters have no arguments passed, the output

But when a second call to the next method, if the incoming parameter, this parameter will be invoked yield method above first return value, so when performing the second yield, is the value of the variable parameters passed inposter

If you want to have a yield value of the first call, you need to pass parameters when you create a generator iterator

const c1=cars('丰田');

 

Guess you like

Origin blog.csdn.net/mynewdays/article/details/93847518