function.next meta-property proposal
In Python, sending a value other than None
into the first invocation of
send
raises an error. That seems like a reasonable behaviour to me,
without introducing too much weirdness.
TypeError: can't send non-None value to a just-started generator
Alexander, ES6 generators accept any arbitrary values for the first invocation of "next". That's not going to change.
I don't think that's a good enough reason by itself to make this observable. You can pass arbitrary numbers of arguments to any function in JS, and they are generally ignored.
Alexander ES6 "is out" as it is, here the proposal is to make reachable that argument because indeed, since about ever, in JS you can ignore arguments, but you can also always reach them through the argument object.
When it comes to generators, arguments object would be a misleading place to give you any sent value 'cause that's rather how you initialize the generator, hence the function.nect proposal, and I must say I like it in all its weird glory.
Like you said, Python throws, meaning devs coming from Python won't ever
make such mistake of sending values a tthe very first .next
invoke, so JS
early adopters know thanks to all bounce of early articles regarding the
early specs :D
My only concern, is that the "famous" Promise based generator wrap www.promisejs.org/generators/#both needs an update:
function async(makeGenerator){
return function (value) { // <= here
var generator = makeGenerator.apply(this, arguments);
function handle(result){
// result => { done: [Boolean], value: [Object] }
if (result.done) return Promise.resolve(result.value);
return Promise.resolve(result.value).then(function (res){
return handle(generator.next(res));
}, function (err){
return handle(generator.throw(err));
});
}
try {
return handle(generator.next(value)); // <= here
} catch (ex) {
return Promise.reject(ex);
}
}
}
Best
actually, that won't work ... so yeah, that pattern is somehow compromise, arguments is used to indeed initialize the generator, no way to put first value in ... oh well
At the March TC39 meeting we agreed that the
function.next
metapropty should be made into a standalone stage 1 proposal.That proposal is now available at: allenwb/ESideas/blob/master/Generator metaproperty.md