Home
About
Resume
Projects
Links
Blog
Back to Contents
# JavaScript Generator (function*) The function* declaration (function keyword followed by an asterisk) defines a generator function, which returns a Generator object. #### First Seen ```javascript function* work() { // server return array dataUsers const newUsers = [...users].concat([{ name: "Any", age: 39 }]); yield put(actionSuccess({ users: newUsers })); } function* watch() { yield takeLatest(someAction.type, work); } export default function* rootSaga() { yield all([watch()]); } ``` from [React Redux Example](https://codesandbox.io/s/redux-toolkit-state-new-array-forked-fgeym?file=/src/redux/sagas/sagas.js) #### JavaScript Demo: Statement - Function* ```javascript function* generator(i) { yield i; yield i + 10; } const gen = generator(10); console.log(gen.next().value); // Expected output: 10 console.log(gen.next().value); // Expected output: 20 ``` #### Syntax ```javascript function* name(param0) { statements } function* name(param0, param1) { statements } function* name(param0, param1, /* … ,*/ paramN) { statements } ``` #### Source **Reference:** [Mozilla JavaScript function*](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*)
Previous Post:
File Open Flags
Next Post:
JavaScript Optional Chaining
Loading