Created
May 6, 2019 19:45
-
-
Save kosich/382aab1cc645b933e23c800f784b09aa to your computer and use it in GitHub Desktop.
mergeMap + forkJoin with accumulation example
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 { rxObserver } = require('api/v0.3'); | |
const { forkJoin, timer, of } = require('rxjs'); | |
const { map, mapTo, mergeMap } = require('rxjs/operators'); | |
const parentTasksToSave = 1; | |
const childTasksToSave = [2, 3]; | |
createWorkItem(parentTasksToSave).pipe( | |
mergeMap(parentId => { | |
if (parentId === null) { | |
return of([ parentId ]); | |
} | |
const childTaskObservables = childTasksToSave.map( | |
childId => createChildWorkItem(childId) | |
); | |
return forkJoin(childTaskObservables).pipe( | |
map(ids => [parentId, ...ids]) | |
); | |
}) | |
) | |
.subscribe(rxObserver()); | |
// MOCK that will create work item in 300ms | |
function createWorkItem(id){ | |
return timer(300).pipe( | |
mapTo(id) | |
); | |
} | |
// MOCK that will create child work item in 150ms | |
function createChildWorkItem(id) { | |
return timer(150).pipe( | |
mapTo(id) | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment