Skip to content

Instantly share code, notes, and snippets.

@meredian
Created October 27, 2016 06:09
Show Gist options
  • Save meredian/8b05a08ca4f0daab8a78f48e51fe2b34 to your computer and use it in GitHub Desktop.
Save meredian/8b05a08ca4f0daab8a78f48e51fe2b34 to your computer and use it in GitHub Desktop.
function asynchronousIterator(limit, f1) {
var sum = 0;
var limit = 3;
fAsync(limit, function(res) {
console.log("Intermediate result", res);
sum += res;
limit--;
if(limit === 0){
f1(sum);
}
})
}
function fAsync(x, f2) {
var result = x * x;
setTimeout(function(){
f2(result);
}, Math.random() * 100);
x--;
if(x > 0){
fAsync(x,f2);
}
}
asynchronousIterator(5, function(a){
console.log(a);
});
➜ i3bitrix-test git:(master) ✗ node synh_vs_an.js
Intermediate result 4
Intermediate result 9
Intermediate result 1
14
➜ i3bitrix-test git:(master) ✗ node synh_vs_an.js
Intermediate result 1
Intermediate result 4
Intermediate result 9
14
➜ i3bitrix-test git:(master) ✗ node synh_vs_an.js
Intermediate result 9
Intermediate result 1
Intermediate result 4
14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment