Why doesn't the generator running asynchronous task?

Trying to learn generators, wrote an example:
function * getData(){
 var callback = function * (data,err) {
if(err){
 yield err;
}
if(data){
 yield data;
}
};
 var x = $.ajax({
 url: 'http://localhost:9001/mydata',
 success: callback
});
}
var d = getData();

If I do d.next() will return undefined. And I want to make it so that when after var d = getData() in a variable d was the content of the page /mydata. Poke me error how to do it properly?
July 9th 19 at 11:24
1 answer
July 9th 19 at 11:26
not the full ajax options
url: url,
async: true,
cache: false,

Another scope is broken.
async: true and cache: false - optional parameters.
You can show the right decision? - tracey commented on July 9th 19 at 11:29
Exactly parallel queries?
async(function *() {
try {
var result1 = $.ajax("request1");
var result2 = $.ajax("request2");
var result3 = $.ajax("request3");
// Process the result
let results = {"1": yield result1, "2": yield result2, "3": yield result3}
console.log(results);
} catch(xhr) {
console.log("Error:" + xhr);
}
});
Somewhere like this, I write from the phone can't check. - Mortimer commented on July 9th 19 at 11:32
I saw this example, you copied and pasted here, I meant an example based on my above piece of code, it will give more clear understanding - tracey commented on July 9th 19 at 11:35
Well, do you not know that there is data. As success can be either true or false, function * (data)
{
if(data)
{
yield data;
} else {
// Then return an error. Or even the first couple may not be.

Check. If anything, we will continue to look. - Mortimer commented on July 9th 19 at 11:38
: Have done so:
function * getData(url){
 var xhr = new XMLHttpRequest();
 xhr.open('GET', url, true);

 xhr.onload = function * () {
 yield this.response;
};

xhr.send();
};

var d = getData('/mydata');

console.log(d)

But also not it! - tracey commented on July 9th 19 at 11:41

Find more questions by tags JavaScript