Skip to content
This repository was archived by the owner on Apr 20, 2018. It is now read-only.

Commit 11412ce

Browse files
Merge pull request #711 from xgrommx/patch-2
Update when.md
2 parents 4818702 + 0ca1d2c commit 11412ce

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

doc/api/core/operators/when.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,39 @@ var subscription = source.subscribe(
3333
// => Completed
3434
```
3535

36+
#### Example
37+
38+
```js
39+
var chopsticks = [new Rx.Subject(), new Rx.Subject(), new Rx.Subject()];
40+
41+
var hungry = [new Rx.Subject(), new Rx.Subject(), new Rx.Subject()];
42+
43+
var eat = i => {
44+
return () => {
45+
setTimeout(() => {
46+
console.log('Done');
47+
chopsticks[i].onNext({});
48+
chopsticks[(i+1) % 3].onNext({});
49+
}, 1000);
50+
return 'philosopher ' + i + ' eating';
51+
};
52+
};
53+
54+
var dining = Rx.Observable.when(
55+
hungry[0].and(chopsticks[0]).and(chopsticks[1]).thenDo(eat(0)),
56+
hungry[1].and(chopsticks[1]).and(chopsticks[2]).thenDo(eat(1)),
57+
hungry[2].and(chopsticks[2]).and(chopsticks[0]).thenDo(eat(2))
58+
);
59+
60+
dining.subscribe(console.log.bind(console));
61+
62+
chopsticks[0].onNext({}); chopsticks[1].onNext({}); chopsticks[2].onNext({});
63+
64+
for (var i = 0; i < 3; i++) {
65+
hungry[0].onNext({}); hungry[1].onNext({}); hungry[2].onNext({});
66+
}
67+
```
68+
3669
### Location
3770

3871
File:

0 commit comments

Comments
 (0)