Skip to content

Instantly share code, notes, and snippets.

@LoLei
Created September 7, 2017 15:45
Show Gist options
  • Select an option

  • Save LoLei/e6e3bd2d9886566793f819602c0c136b to your computer and use it in GitHub Desktop.

Select an option

Save LoLei/e6e3bd2d9886566793f819602c0c136b to your computer and use it in GitHub Desktop.
JS Bin // source https://jsbin.com/zakoqan
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
"use strict";
var loop = function loop(i, array, increment) {
var promises = [];
for (var j = 0; j < increment; j++) {
console.log(array[i + j]);
// promises.push(save(array[i + j]));
var p = new Promise(function (resolve, reject) {
setTimeout(function () {
var x = array[i + j];
console.log("Done with saving: " + x);
resolve(x);
}, 250, 'foo');
});
promises.push(p);
}
Promise.all(promises).then(function (res) {
console.log(res);
console.log("Done with " + increment + " promises, done " + parseInt(i / 10 + 1) + " times.");
});
};
console.log("start");
var array = [];
for (var j = 0; j < 1000; j++) {
array.push(j);
}
var amount = array.length;
var increment = 10;
for (var it = 0; it < amount; it += increment) {
loop(it, array, increment);
}
console.log("end");
function save(x) {
setTimeout(function () {
console.log("Done saving: " + x);
return Promise.resolve(1);
}, 250);
}
</script>
<script id="jsbin-source-javascript" type="text/javascript">var loop = function(i, array, increment) {
var promises = [];
for (var j = 0; j < increment; j++) {
console.log(array[i + j]);
// promises.push(save(array[i + j]));
var p = new Promise(function(resolve, reject) {
setTimeout(function(){
var x = array[i + j]
console.log("Done with saving: " + x);
resolve(x);
}, 250, 'foo');
});
promises.push(p);
}
Promise.all(promises).then(function(res){
console.log(res);
console.log("Done with " + increment + " promises, done " + parseInt((i/10)+1) + " times.");
});
}
console.log("start");
var array = [];
for (var j = 0; j < 1000; j++) {
array.push(j);
}
var amount = array.length;
var increment = 10;
for (var it = 0; it < amount; it+=increment) {
loop(it, array, increment);
}
console.log("end");
function save(x) {
setTimeout(function(){
console.log("Done saving: " + x);
return Promise.resolve(1);
}, 250);
}</script></body>
</html>
"use strict";
var loop = function loop(i, array, increment) {
var promises = [];
for (var j = 0; j < increment; j++) {
console.log(array[i + j]);
// promises.push(save(array[i + j]));
var p = new Promise(function (resolve, reject) {
setTimeout(function () {
var x = array[i + j];
console.log("Done with saving: " + x);
resolve(x);
}, 250, 'foo');
});
promises.push(p);
}
Promise.all(promises).then(function (res) {
console.log(res);
console.log("Done with " + increment + " promises, done " + parseInt(i / 10 + 1) + " times.");
});
};
console.log("start");
var array = [];
for (var j = 0; j < 1000; j++) {
array.push(j);
}
var amount = array.length;
var increment = 10;
for (var it = 0; it < amount; it += increment) {
loop(it, array, increment);
}
console.log("end");
function save(x) {
setTimeout(function () {
console.log("Done saving: " + x);
return Promise.resolve(1);
}, 250);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment