A little doubt about Generator

encountered a problem of generator function while learning knowledge such as generator/promise/async-await in js:

var fetch = require("node-fetch");

function* gen(){
  var url = "https://api.github.com/users/github";
  var result = yield fetch(url);
  console.log(result.bio);
}

this code describes the use of a simple generator that needs to return a value of then (), to the promise obtained by the next () function multiple times to get the final json variable, which is passed into the second next () function.

you can see that the result variable in the code is that promise, while result.bio actually does not exist, or it is wrong.
should be result.then (.). Then (.). Bio .
[I wonder if what I am saying here is correct? ]

so with regard to the automatic execution of generator, the way to use async-await is to actually get the return value of multiple promise (the value when it is successful) and finally pass it to the next next () function. The use of the value of the yield statement (result in the above code) after yield in the content of the generator function is actually replacing it with the final value.
[ I wonder if what I am saying here is correct? ]

finally, the use of generator and async-await is to convert the asynchronous promise.then () function into Synchronize.
[ I wonder if what I am saying here is correct? ]

I wonder if there is anything wrong with my understanding.
if there is a better way to understand or related articles, also thank you for instructions or instructions.


I wonder if you are learning from Ruan Yifeng's introduction to es6 . This part is very clear.
this code is called as follows:

var g = gen();
var result = g.next();

result.value.then(function(data){
  return data.json();
}).then(function(data){
  g.next(data);
});

yield itself does not return a value, and when the next method takes an argument, that parameter is treated as the return value of the previous yield expression. console.log (result.bio); when calling next (), the result here should be undefined;. When calling next (data), the result here is data.
"the async function can be thought of as multiple asynchronous operations, wrapped into a Promise object, and the await command is the syntax sugar of the internal then command." After all the await commands within the
async function are executed, the final result of return is returned to the callback function of Promise.then. Example in getting started with es6:

async function getStockPriceByName(name) {
  const symbol = await getStockSymbol(name);
  const stockPrice = await getStockPrice(symbol);
  return stockPrice;
}

getStockPriceByName('goog').then(function (result) {
  console.log(result);  // stockPrice
});
Menu