-
-
Save a-laughlin/628bd2c28fc89f6f5117 to your computer and use it in GitHub Desktop.
JS Bin 3 ways to do Promise.all(asyncVals) // source http://jsbin.com/wegoha/14
This file contains 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 name="description" content="3 ways to do Promise.all(asyncVals)"> | |
<script src="https://cdn.rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.6/rx.all.js"></script> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
</head> | |
<body> | |
<script id="jsbin-javascript"> | |
'use strict'; | |
console.log('3 ways to do Promise.all(asyncVals)'); | |
var log = console.log.bind(console); | |
var start = Date.now(); | |
var iter = 0; | |
var valsArray = ['a', 'b', 'c'].map(function (val, i) { | |
return Rx.Observable.create(function (obs) { | |
var tid = setTimeout(function () { | |
var combinedVal = val + iter++; | |
log(combinedVal + ' ' + (Date.now() - start) + 'ms'); | |
obs.onNext(combinedVal); | |
obs.onCompleted(combinedVal); | |
}, i * 500); | |
return function () { | |
return clearTimeout(tid); | |
}; | |
}); | |
// .repeat(2) | |
// .share() | |
}); | |
var hot$ = Rx.Observable['for'](valsArray).repeat(2).share(); | |
// Obs.for(arr) === Obs.from(arr).flatMap(obj=>obj); | |
hot$.bufferWithCount(valsArray.length).subscribe(function (vals) { | |
return log('hot$.bufferWithCount', vals); | |
}); | |
hot$.reduce(function (acc, next, i) { | |
return acc.concat(next); | |
}, []).subscribe(function (vals) { | |
return log('hot$.reduce', vals); | |
}); | |
hot$.toArray().subscribe(function (vals) { | |
return log('hot$.toArray', vals); | |
}); | |
</script> | |
<script id="jsbin-source-javascript" type="text/javascript">console.log('3 ways to do Promise.all(asyncVals)'); | |
let log = console.log.bind(console); | |
let start = Date.now() | |
let iter = 0; | |
let valsArray = ['a','b','c'].map((val,i)=>{ | |
return Rx.Observable.create((obs)=>{ | |
let tid = setTimeout(()=>{ | |
let combinedVal = val+(iter++); | |
log(combinedVal + ' ' + (Date.now()-start)+'ms'); | |
obs.onNext(combinedVal); | |
obs.onCompleted(combinedVal); | |
},i*500); | |
return ()=>clearTimeout(tid); | |
}) | |
// .repeat(2) | |
// .share() | |
}); | |
let hot$ = Rx.Observable.for(valsArray) | |
.repeat(2) | |
.share() | |
// Obs.for(arr) === Obs.from(arr).flatMap(obj=>obj); | |
hot$.bufferWithCount(valsArray.length) | |
.subscribe(vals=>log('hot$.bufferWithCount',vals)); | |
hot$.reduce((acc,next,i)=>acc.concat(next),[]) | |
.subscribe(vals=>log('hot$.reduce',vals)); | |
hot$.toArray() | |
.subscribe(vals=>log('hot$.toArray',vals));</script></body> | |
</html> |
This file contains 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
'use strict'; | |
console.log('3 ways to do Promise.all(asyncVals)'); | |
var log = console.log.bind(console); | |
var start = Date.now(); | |
var iter = 0; | |
var valsArray = ['a', 'b', 'c'].map(function (val, i) { | |
return Rx.Observable.create(function (obs) { | |
var tid = setTimeout(function () { | |
var combinedVal = val + iter++; | |
log(combinedVal + ' ' + (Date.now() - start) + 'ms'); | |
obs.onNext(combinedVal); | |
obs.onCompleted(combinedVal); | |
}, i * 500); | |
return function () { | |
return clearTimeout(tid); | |
}; | |
}); | |
// .repeat(2) | |
// .share() | |
}); | |
var hot$ = Rx.Observable['for'](valsArray).repeat(2).share(); | |
// Obs.for(arr) === Obs.from(arr).flatMap(obj=>obj); | |
hot$.bufferWithCount(valsArray.length).subscribe(function (vals) { | |
return log('hot$.bufferWithCount', vals); | |
}); | |
hot$.reduce(function (acc, next, i) { | |
return acc.concat(next); | |
}, []).subscribe(function (vals) { | |
return log('hot$.reduce', vals); | |
}); | |
hot$.toArray().subscribe(function (vals) { | |
return log('hot$.toArray', vals); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment