Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script src="https://unpkg.com/@reactivex/rxjs@5.0.0-beta.7/dist/global/Rx.umd.js"></script>
CLICK ME
</body>
</html>
 
console.clear();
console.log('Demos the merge of 2 streams and flattening a higher order observable with switchMap');
const TICKERS = [
  {
    "ticker": "GOOG",
    "code": "vobocu"
  },  
  {
    "ticker": "AAPL",
    "code": "satorac"
  },
  {
    "ticker": "KO",
    "code": "luyacer"
  }
];
const iClick$ = Rx.Observable.fromEvent(document, 'click');
const click$ = iClick$.mapTo({
  "ticker": "WMT",
  "code": "wiyekit"
});
const iTicker$ = Rx.Observable.interval(3000);
const ticker$ = iTicker$.map(i => TICKERS[i % TICKERS.length]);
const merge$ = ticker$.merge(click$);
const request$ = merge$.map((ticker) => {
  return `https://output.jsbin.com/${ticker.code}.js`;
});
const response$ = request$.switchMap((url) => {
  console.log(url);
  return fetch(url).then(res => res.json());
});
                                    
response$.subscribe(res => {
  console.log(`Price for ${res.name} is ${res.price}`);
});
Output

You can jump to the latest bin by adding /latest to your URL

Dismiss x
public
Bin info
danday74pro
0viewers