Skip to content

Commit

Permalink
Small fixes to the generators readme.
Browse files Browse the repository at this point in the history
This could use more work, but at least now it has syntax highlighting and I fixed a misleading typo about SpiderMonkey.
  • Loading branch information
domenic committed Jun 4, 2013
1 parent f587e75 commit 1abc83b
Showing 1 changed file with 37 additions and 32 deletions.
69 changes: 37 additions & 32 deletions examples/async-generators/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/!\ Warning: The behavior described here is likely to be quickly
obseleted by developments in standardization and implementation. Tread
with care.
Expand All @@ -20,49 +19,55 @@ Q's ``async`` function supports both kinds of generators. These
examples will use the ES6 style. See the examples and notes in
[js-1.7](js-1.7/) for more on getting these to work with SpiderMonkey.

function* count() {
var i = 0;
while (true) {
yield i++;
}
```js
function* count() {
var i = 0;
while (true) {
yield i++;
}
}

var counter = count();
count.next() === 0;
count.next() === 1;
count.next() === 2;
var counter = count();
count.next() === 0;
count.next() === 1;
count.next() === 2;
```

``yield`` can also return a value, if the ``send`` method of
a generator is used instead of ``next``.

var buffer = (function* () {
var x;
while (true) {
x = yield x;
}
}());
```js
var buffer = (function* () {
var x;
while (true) {
x = yield x;
}
}());

buffer.send(1) === undefined
buffer.send("a") === 1
buffer.send(2) === "a"
buffer.next() === 2
buffer.next() === undefined
buffer.next() === undefined
buffer.send(1) === undefined;
buffer.send("a") === 1;
buffer.send(2) === "a";
buffer.next() === 2;
buffer.next() === undefined;
buffer.next() === undefined;
```

We can use ``yield`` to wait for a promise to resolve.

var eventualAdd = Q.async(function* (oneP, twoP) {
var one = yield oneP;
var two = yield twoP;
return one + two;
});
```js
var eventualAdd = Q.async(function* (oneP, twoP) {
var one = yield oneP;
var two = yield twoP;
return one + two;
});

eventualAdd(eventualOne, eventualTwo)
.then(function (three) {
three === 3;
});
eventualAdd(eventualOne, eventualTwo)
.then(function (three) {
three === 3;
});
```

To use these in SpiderMonkey, change ``function`` to ``function*``.
To use these in SpiderMonkey, change ``function*`` to ``function``.
Also, in this last example, SpiderMonkey does not allow return values in
generators. To work around that, call the ``Q.return`` function instead
of using a ``return`` statement. ``Q.return`` will go away at some
Expand Down

0 comments on commit 1abc83b

Please sign in to comment.