Skip to content

Instantly share code, notes, and snippets.

@DevWurm
Created April 3, 2017 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DevWurm/45e25abfefeb953da76b08644f939271 to your computer and use it in GitHub Desktop.
Save DevWurm/45e25abfefeb953da76b08644f939271 to your computer and use it in GitHub Desktop.
Promise.all and promise.then(...).then(...) behavior for Observables
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