Skip to content

Instantly share code, notes, and snippets.

@sonthonaxrk
Forked from anonymous/index.html
Last active October 27, 2015 21:42
Show Gist options
  • Save sonthonaxrk/b37e057146a44ba7f9cf to your computer and use it in GitHub Desktop.
Save sonthonaxrk/b37e057146a44ba7f9cf to your computer and use it in GitHub Desktop.
Fetching paginated data in order with BaconJS
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Bin</title>
<script src="https://code.jquery.com/jquery-2.1.3.min.js"></script>
<script src="https://rawgit.com/baconjs/bacon.js/master/dist/Bacon.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.8.0/lodash.js"></script>
</head>
<body>
<script id="jsbin-javascript">
var pages = Bacon.fromArray([1,2,3,4,5])
var requests = pages.flatMapWithConcurrencyLimit(1, function(page) {
return doAjax(page)
.map(function(value) {
return {
page: page,
value: value
}
})
}).log("Data received")
var allData = requests.fold([], function(arr, data) {
return arr.concat([data])
}).map(function(arr) {
// I would normally write this as a oneliner
var sorted = _.sortBy(arr, "page")
var onlyValues = _.pluck(sorted, "value")
var inOneArray = _.flatten(onlyValues)
return inOneArray
})
allData.log("All data")
function doAjax(page) {
// This would actually be Bacon.fromPromise($.ajax...)
// Math random to simulate the fact that requests can return out
// of order
return Bacon.later(1 + Math.random() * 500, [
"Page"+page+"Item1",
"Page"+page+"Item2"])
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">var pages = Bacon.fromArray([1,2,3,4,5])
var requests = pages.flatMapWithConcurrencyLimit(1, function(page) {
return doAjax(page)
.map(function(value) {
return {
page: page,
value: value
}
})
}).log("Data received")
var allData = requests.fold([], function(arr, data) {
return arr.concat([data])
}).map(function(arr) {
// I would normally write this as a oneliner
var sorted = _.sortBy(arr, "page")
var onlyValues = _.pluck(sorted, "value")
var inOneArray = _.flatten(onlyValues)
return inOneArray
})
allData.log("All data")
function doAjax(page) {
// This would actually be Bacon.fromPromise($.ajax...)
// Math random to simulate the fact that requests can return out
// of order
return Bacon.later(1 + Math.random() * 500, [
"Page"+page+"Item1",
"Page"+page+"Item2"])
}</script></body>
</html>
var pages = Bacon.fromArray([1,2,3,4,5])
var requests = pages.flatMapWithConcurrencyLimit(1, function(page) {
return doAjax(page)
.map(function(value) {
return {
page: page,
value: value
}
})
}).log("Data received")
var allData = requests.fold([], function(arr, data) {
return arr.concat([data])
}).map(function(arr) {
// I would normally write this as a oneliner
var sorted = _.sortBy(arr, "page")
var onlyValues = _.pluck(sorted, "value")
var inOneArray = _.flatten(onlyValues)
return inOneArray
})
allData.log("All data")
function doAjax(page) {
// This would actually be Bacon.fromPromise($.ajax...)
// Math random to simulate the fact that requests can return out
// of order
return Bacon.later(1 + Math.random() * 500, [
"Page"+page+"Item1",
"Page"+page+"Item2"])
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment