I suppose it's worth pointing out that there's (technically) no ambiguity
about whether or not a yield expression will result in suspension. It
will. Period.
Granted, sometimes execution may be resumed synchronously, and sometimes
from an async context, but I think we're already starting to see some
patterns that give us some indicators there. Specifically, it's fairly
obvious that the most common approach for async control-flow with
generators is going to be with the assistance of some sort of utility
(e.g., suspend, co, gen-run, galaxy, etc.).
That is, consider the following example:
suspend(function* () {
var user = yield db.getUser('jmar777');
})();
So, we now have:
1) function*
2) yield
3) an intentional call to something returning a promise (or continuable)
4) the whole thing is wrapped in a suspend() call, a library who's sole
purpose is to help with async interactions
I'll concede on an academic level that the syntax itself is ambiguous (but
hey, that's JavaScript). However, that's where the ambiguity starts *and*
ends.
The async/suspend/resume semantics are pretty abundantly clear.
On Tuesday, August 6, 2013 7:31:04 PM UTC-4, Tim Caswell wrote:
>
>
>
>
> On Tue, Aug 6, 2013 at 5:20 PM, Mikeal Rogers
> <[email protected]<javascript:>
> > wrote:
>
>> I think Tim means that the author of the generator should assume it will
>> be suspended at any time, not the consumer.
>>
>
> Yes, that's exactly what I mean. No function that you call can suspend
> you simply by calling it. That danger does not exist in ES6's version of
> generators.
>
>
>
>>
>> On Aug 6, 2013, at 3:17PM, Mark Hahn <[email protected] <javascript:>>
>> wrote:
>>
>> > Always assume that you will be suspended any time you have a yield or
>> yield*
>>
>> How do you know any of this you see a function call? Only the function
>> definition looks different. Tell me which is these will yield ..
>>
>> a()
>> b()
>> c()
>>
>> On Tue, Aug 6, 2013 at 2:54 PM, Tim Caswell <[email protected]<javascript:>
>> > wrote:
>>
>>>
>>>
>>>
>>> On Tue, Aug 6, 2013 at 4:47 PM, Mikeal Rogers
>>> <[email protected]<javascript:>
>>> > wrote:
>>>
>>>> It is the *exact same* hazard as the code you posted, I don't think it
>>>> is any *more* of a hazard at the language level. The difference is that
>>>> the
>>>> pattern in the function code below is basically never seen in node.js code
>>>> and it has been *possible* to write it since node's creation.
>>>>
>>>> Could be that nobody will use/clobber closure scope in their generators
>>>> either, but I'm afraid they will because it will work and operate as
>>>> expected *most* of the time and, unlike the function example you've
>>>> posted,
>>>> it will still fit well when used with what I assume will become a healthy
>>>> ecosystem of generator consuming libraries.
>>>>
>>>> The reason nobody writes this kind of thing in node is that it just
>>>> won't play well with the rest of the libraries in the ecosystem whereas
>>>> the
>>>> hazardous generator example looks like it would fit just as well as a
>>>> "well
>>>> written" generator until it hits scale and causes visible bugs.
>>>>
>>>> All kinds of bad patterns and bad code are possible in any language.
>>>> The patterns we adopt to create a healthy ecosystem are what will keep
>>>> people away from the third rails. Depending on people to have "well
>>>> written" code, by some definition of "well", is not a good alternative to
>>>> solidifying and encouraging patterns that make it *difficult* to write
>>>> hazardous code in the first place.
>>>>
>>>> No pattern or feature in JavaScript escapes the potential for abuse and
>>>> bugs, and nothing that I'm worried about w/ respect to generators is worse
>>>> or better than language level hazards w/ callbacks, it's just that we've
>>>> created a set of patterns that *discourage* those hazards in the current
>>>> ecosystem and I'm failing to see how we do that in this particular case
>>>> with generators.
>>>>
>>>
>>> I propose a simple rule for all generator code:
>>>
>>> - Always assume that you will be suspended any time you have a yield or
>>> yield* and any shared state may change.
>>>
>>> If we educate everyone and focus on that one thing (yield) that will
>>> cover all hazards around shared state and generators, be it async
>>> promise/continuable based stuff or sync lazy iterator stuff.
>>>
>>> Just like with functions we've learned to look for the "function"
>>> keyword and guard our state around that point, we need to do the same
>>> around the "yield" keyword.
>>>
>>> Teach people correct principles and trust them to do the right thing.
>>> We'll be fine.
>>>
>>>
>>>
>>>>
>>>> -Mikeal
>>>>
>>>> On Aug 6, 2013, at 2:33PM, Tim Caswell <[email protected]<javascript:>>
>>>> wrote:
>>>>
>>>> My question to people on the list is how is this any different than the
>>>> same hazard with normal function?
>>>>
>>>> var sum, i;
>>>> function reduce(arr) {
>>>> sum = 0;
>>>> i = 0;
>>>> return function () {
>>>> if (i < arr.length) {
>>>> sum += arr[i++];
>>>> return sum;
>>>> }
>>>> };
>>>> }
>>>>
>>>>
>>>>
>>>> --
>>>> --
>>>> Job Board: http://jobs.nodejs.org/
>>>> Posting guidelines:
>>>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>>>> You received this message because you are subscribed to the Google
>>>> Groups "nodejs" group.
>>>> To post to this group, send email to [email protected]<javascript:>
>>>> To unsubscribe from this group, send email to
>>>> [email protected] <javascript:>
>>>> For more options, visit this group at
>>>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>>>
>>>> ---
>>>> You received this message because you are subscribed to the Google
>>>> Groups "nodejs" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected] <javascript:>.
>>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>>
>>>>
>>>>
>>>
>>>
>>> --
>>> --
>>> Job Board: http://jobs.nodejs.org/
>>> Posting guidelines:
>>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>>> You received this message because you are subscribed to the Google
>>> Groups "nodejs" group.
>>> To post to this group, send email to [email protected]<javascript:>
>>> To unsubscribe from this group, send email to
>>> [email protected] <javascript:>
>>> For more options, visit this group at
>>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>>
>>> ---
>>> You received this message because you are subscribed to the Google
>>> Groups "nodejs" group.
>>> To unsubscribe from this group and stop receiving emails from it, send
>>> an email to [email protected] <javascript:>.
>>> For more options, visit https://groups.google.com/groups/opt_out.
>>>
>>>
>>>
>>
>>
>> --
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]<javascript:>
>> To unsubscribe from this group, send email to
>> [email protected] <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>>
>> --
>> --
>> Job Board: http://jobs.nodejs.org/
>> Posting guidelines:
>> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
>> You received this message because you are subscribed to the Google
>> Groups "nodejs" group.
>> To post to this group, send email to [email protected]<javascript:>
>> To unsubscribe from this group, send email to
>> [email protected] <javascript:>
>> For more options, visit this group at
>> http://groups.google.com/group/nodejs?hl=en?hl=en
>>
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "nodejs" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected] <javascript:>.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>>
>
>
--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.