Created
June 24, 2021 13:01
-
-
Save VictorCoding/ee448e7b882b8e6d4158019b754b40d8 to your computer and use it in GitHub Desktop.
execute array of observables in a sequence
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 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