I want to return a generator for the cars being created. The issue is, I nest an API to receive some random colours for the cars upon which I cannot yield
to since they're not declared as generator functions.
I tried to create the reference using let car
but because the request is async
, it yields before the car is instanced. Perhaps I need to pass this as reference? Any ideas?
static async api()
{
return (await fetch('http://www.colr.org/json/colors/random/8')).json();
}
static* createCars(n)
{
for(let i = 1; i <= n; i++) {
Car.api().then(resp => {
let car = (new self({
x: 0,
y: 250,
colour: "#" + resp.colors[3].hex,
windowsColour: "#" + resp.colors[2].hex,
number: i
})).draw();
});
yield car;
}
}
Uncaught ReferenceError: car is not defined