Created
October 27, 2015 21:41
-
-
Save anonymous/08f806c8bc9f32d86e10 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/fogozijeha
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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