I wander if it's possible to use arrow function with generators like this:
app.use( *() => {
...body of function
});
Thanks!
I wander if it's possible to use arrow function with generators like this:
app.use( *() => {
...body of function
});
Thanks!
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*
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!