Skip to content

Instantly share code, notes, and snippets.

@VictorCoding
Created June 24, 2021 13:01
Show Gist options
  • Save VictorCoding/ee448e7b882b8e6d4158019b754b40d8 to your computer and use it in GitHub Desktop.
Save VictorCoding/ee448e7b882b8e6d4158019b754b40d8 to your computer and use it in GitHub Desktop.
execute array of observables in a sequence
const obsone = () =>
new Observable((obs) => {
setTimeout(() => {
console.log('one');
obs.next();
}, 1000);
});
const obstwo = () =>
new Observable((obs) => {
setTimeout(() => {
console.log('two');
obs.next();
}, 1000);
});
function runSequence() {
return [obsone, obstwo].reduce((accumulator, next) => {
return new Observable(obs => {
accumulator.subscribe(() => {
next().subscribe(() => {
obs.next();
});
});
});
}, of(1));
}
runSequence().subscribe(() => {
console.log('done');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment