Skip to content

Instantly share code, notes, and snippets.

@kosich
Created May 6, 2019 19:45
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 kosich/382aab1cc645b933e23c800f784b09aa to your computer and use it in GitHub Desktop.
Save kosich/382aab1cc645b933e23c800f784b09aa to your computer and use it in GitHub Desktop.
mergeMap + forkJoin with accumulation example
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