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.