Generator, Iterator, Promise & (async/await) relationship

The generator is based on the Iterator specification to manage promises or asynchronous programming; Promises manage asynchronous programming based on a promise pattern async/await is a further encapsulation of the generator

The generator to realize

Create an instance of func, but unlike new, the code in func is not executed. The code will be executed only after ite.next () is executed, and each execution of next will end at yield. Each iteration will return {done: True /false, value:yield or return value}

function* func() {
    console.log('A');
    yield 10;
    console.log('B');
    yield 20;
    console.log('C');
    yield 30;
    console.log('4');
    return 40;
}
let iter = func([10.20.30.40]);
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());
console.log(iter.next());

Copy the code

When you perform next, you can pass the value as the result of the last yield, but the yield is for the value after each next execution