Skip to content

Instantly share code, notes, and snippets.

@joeoravec
Created September 28, 2017 14:27
Show Gist options
  • Save joeoravec/7684aa8a85e0e6f0d06e1afc173f72a0 to your computer and use it in GitHub Desktop.
Save joeoravec/7684aa8a85e0e6f0d06e1afc173f72a0 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/xepafat
<!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/[email protected]/dist/global/Rx.js"></script>
<script id="jsbin-javascript">
/*
From egghead.io lesson on using forkJoin
Gets array of ids from hnUrl and uses forkJoin to flatten
the results from subsequest ajax requests into an
array of result objects.
*/
'use strict';
var _Rx = Rx;
var Observable = _Rx.Observable;
var hnUrl = 'https://hacker-news.firebaseio.com/v0/topstories.json';
var itemUrl = function itemUrl(id) {
return 'https://hacker-news.firebaseio.com/v0/item/' + id + '.json';
};
Observable.ajax(hnUrl).map(function (data) {
return data.response.slice(0, 3);
}).switchMap(function (ids) {
return Observable.forkJoin(ids.map(function (id) {
var url = itemUrl(id);
return Observable.ajax(url).map(function (e) {
return e.response;
});
}));
}).subscribe(function (result) {
return console.log(result);
});
</script>
<script id="jsbin-source-javascript" type="text/javascript">/*
From egghead.io lesson on using forkJoin
Gets array of ids from hnUrl and uses forkJoin to flatten
the results from subsequest ajax requests into an
array of result objects.
*/
const {Observable} = Rx;
const hnUrl = 'https://hacker-news.firebaseio.com/v0/topstories.json';
const itemUrl = id => `https://hacker-news.firebaseio.com/v0/item/${id}.json`
Observable.ajax(hnUrl).map(data => data.response.slice(0,3))
.switchMap(ids => Observable.forkJoin(ids.map(id => {
const url = itemUrl(id);
return Observable.ajax(url).map(e => {
return e.response;
})
})))
.subscribe(result => console.log(result))</script></body>
</html>
/*
From egghead.io lesson on using forkJoin
Gets array of ids from hnUrl and uses forkJoin to flatten
the results from subsequest ajax requests into an
array of result objects.
*/
'use strict';
var _Rx = Rx;
var Observable = _Rx.Observable;
var hnUrl = 'https://hacker-news.firebaseio.com/v0/topstories.json';
var itemUrl = function itemUrl(id) {
return 'https://hacker-news.firebaseio.com/v0/item/' + id + '.json';
};
Observable.ajax(hnUrl).map(function (data) {
return data.response.slice(0, 3);
}).switchMap(function (ids) {
return Observable.forkJoin(ids.map(function (id) {
var url = itemUrl(id);
return Observable.ajax(url).map(function (e) {
return e.response;
});
}));
}).subscribe(function (result) {
return console.log(result);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment