Created
April 3, 2017 08:19
-
-
Save DevWurm/45e25abfefeb953da76b08644f939271 to your computer and use it in GitHub Desktop.
Promise.all and promise.then(...).then(...) behavior for Observables
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
const create = (val) => { | |
return (o: Observer<any>) => { | |
setTimeout(() => { | |
console.log(val); | |
o.next(val); | |
o.complete(); | |
}, 5000) | |
} | |
}; | |
const f1 = () => { | |
return Observable.create(create(1)); | |
}; | |
const f2 = () => { | |
return Observable.create(create(2)); | |
}; | |
const f3 = () => { | |
return Observable.create(create(3)); | |
}; | |
// promise.then(...).then(...) | |
f1().flatMap(f2).flatMap(f3).subscribe((vals) => console.log("Subscribe:", vals)); | |
// Promise.all | |
Observable.forkJoin(...[f1(), f2(), f3()]).subscribe((vals) => console.log("Subscribe:", vals)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment