I want to destructure the result of a previous yield using default values when the object is empty. But I'm getting a Cannot read property 'xxx' of undefined, meaning that where I try to destructure the variable theObject is undefined, but why?
const DEFAULT_POSITION = {x: 20, y: 20}
const myObject = {}
function* myGenerator(i) {
const theObject = yield myObject;
const { posX = DEFAULT_POSITION.x, posY = DEFAULT_POSITION.y, scale = 1 } = theObject
yield {posX, posY, scale}
}
The first yield returns me an empty object as expected, but then when I run the generator again I get the error that the first item (posX) in the object destruction can not be read since the theObject is undefined.