Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions Generator metaproperty.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
## Generator function.sent Meta Property ##
## Generator `function.sent` Meta Property ##
Allen Wirfs-Brock
May 13, 2015

### The Problem
When the `next` method is invoked on a generator objects, the value passed as the first argument to `next` is "sent" to the generator object and becomes available within the body of the generator function as the value of the `yield` expression that most recently suspended the generator function. This supports two-way communications between the a generator object and its consumer.
When the `next` method is invoked on a generator object, the value passed as the first argument to `next` is "sent" to the generator object and becomes available within the body of the generator function as the value of the `yield` expression that most recently suspended the generator function. This supports two-way communications between the generator object and its consumer.

However, the first `next` that a generator's consumer invokes to start a generator object does not correspond to any `yield` within the body of the generator function. Instead, the first `next` simply causes execution of the generator function body to begin at the top of the body.

Because there the first `next` call does not correspond to a `yield` within the generator function body there is currently no way for the code with the body to access the initial `next` argument. For example:
Because the first `next` call does not correspond to a `yield` within the generator function body there is currently no way for the code with the body to access the initial `next` argument. For example:

```js
function *adder(total=0) {
let increment=1;
let increment = 1;
while (true) {
switch (request = yield total += increment) {
case undefined: break;
Expand All @@ -25,32 +25,32 @@ let tally = adder();
tally.next(0.1); // argument will be ignored
tally.next(0.1);
tally.next(0.1);
let last=tally.next("done");
console.log(last.value); //1.2 instead of 0.3
let last = tally.next("done");
console.log(last.value); // 1.2 instead of 0.3
```
In the above example, the argument to the `next` method normally supplies the value to added to a running tally. Except that the increment value supplied to the first next is ignored.
In the above example, the argument to the `next` method normally supplies the value to added to a running tally. Except that the increment value supplied to the first `next` is ignored.

This proposal provides an alternative way to access the `next` parameter that works on the first and all subsequent invocations of a generator's `next` method.
### The Proposal

###A new meta-property: `function.sent`
#####Value and Context
The value of `function.sent` within the body of a Generator Function is the value passed to the generator by the `next` method that most recently resumed execution of the generator. In particular, referencing `function.sent` prior to the first evaluation of a `yield` operator returns the argument value passed by the `next` call that started evaluation of the *GeneratorBody*.
The value of `function.sent` within the body of a Generator Function is the value passed to the generator by the `next` method that most recently resumed execution of the generator. In particular, referencing `function.sent` prior to the first evaluation of a `yield` operator returns the argument value passed by the `next` call that started evaluation of the *GeneratorBody*.

`function.sent` can appear anywhere a *YieldExpress* would be legal. Referencing `function.sent` outside of a *GeneratorBody* is a Syntax Error.
#####Usage Example
Here is how the above example might be rewritten using `function.sent`
```js
function *adder(total=0) {
let increment=1;
let increment = 1;
do {
switch (request = function.sent){
switch (request = function.sent) {
case undefined: break;
case "done": return total;
default: increment = Number(request);
}
yield total += increment;
} while (true)
} while (true);
}

let tally = adder();
Expand Down