I wander if it's possible to use arrow function with generators like this:
app.use( *() => {
...body of function
});
Thanks!
2 Answers 2
No, it's not possible to make it shorter than described in the documentation:
function* name([param[, param[, ... param]]]) {
statements
}
Ref: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*
Comments
You can, but not really in a nice way. It's not shorter and does not look that pretty. Check this out:
function* iterable(arg) {
yield* [];
}
async function* asyncIterable(arg) {
yield* [];
}
const arrowIterable = arg => {
return {
*[Symbol.iterator]() {
yield* [];
},
};
};
const arrowAsyncIterable = arg => {
return {
async *[Symbol.asyncIterator]() {
yield* [];
},
};
};
This works because an iterable is basically an object with the Symbol.iterator or Symbol.asyncIterator set to an iterator. A generator is an iterator!
Enjoy!
Comments
Explore related questions
See similar questions with these tags.
app.use(function*() { /* body of generator */ }). More discuss here: stackoverflow.com/a/34108006/7878274*() => {}" or "()* => {}and so on.. You see? It's difference to be smart and think you're smart...