Skip to content

Commit

Permalink
Update README, fix most.generate example in docs
Browse files Browse the repository at this point in the history
  • Loading branch information
briancavalier committed Feb 25, 2015
1 parent e8d14d7 commit 74898e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ most.fromPromise(Promise.resolve('hello'))

Conceptually, [generators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function*) allow you to write a function that acts like an iterable sequence. Generators support the standard ES6 [Iterator interface](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/The_Iterator_protocol), so they can be iterated over using ES6 standard `for of` or the iterator's `next()` API.

Most.js interoperates with ES6 generators and iterators. For example, you can create an observable stream from any ES6 iterable:
Most.js interoperates with ES6 generators and iterators. For example, you can create an event stream from any ES6 iterable:

```js
function* allTheIntegers() {
Expand All @@ -132,3 +132,23 @@ most.from(allTheIntegers())
.take(100)
.observe(x => console.log(x));
```

You can also create an event stream from an *asynchronous generator*, a generator that yields promises:

```js
function* allTheIntegers(interval) {
let i=0;
while(true) {
yield delayPromise(interval, i++);
}
}

function delayPromise(ms, value) {
return new Promise(resolve => setTimeout(() => resolve(value), ms));
}

// Log the first 100 integers, at 1 second intervals
most.generate(allTheIntegers, 1000)
.take(100)
.observe(x => console.log(x));
```
2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ When the generator yields a promise, the promise's fulfillment value will be add

```js
function delayPromise(ms, value) {
return new Promise(resolve => setTimeout(() => resolve(value), delay));
return new Promise(resolve => setTimeout(() => resolve(value), ms));
}

function* countdownGet(delay, start) {
Expand Down

0 comments on commit 74898e5

Please sign in to comment.