Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passing parameters into generator functions

Tags:

javascript

I'm learning generator functions in the codebase (redux sagas) and I'm seeing parameters passed in like

export function* someGenerator({ x, y = {} }) {

when I run it like

someGenerator('xyz', 'abc')

as with a regular function it comes out as undefined

I don't get the ({}) part or how I can pass a parameter into such a function.

thing is I'm trying to call a generator function within another one

like image 929
totalnoob Avatar asked Nov 24 '25 19:11

totalnoob


1 Answers

The function is using Default Object properties. It is doing two things:

  • Destructuring the property x if the object with property x is provided to the function as first argument.
  • Destructuring the property y of the given object. If there is not property y of the parameter then it will be assigned to empty object.

See the below examples:

function someGenerator({ x, y = {} }) {
  console.log(x)
  console.log(y)
}

someGenerator({x:'xyz',y:'abc'})

someGenerator({x:'xyz'}) // 'y' will empty object.
like image 84
Maheer Ali Avatar answered Nov 27 '25 08:11

Maheer Ali



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!