Skip to content

Instantly share code, notes, and snippets.

@victorkurauchi
Last active March 4, 2020 09:45
Show Gist options
  • Save victorkurauchi/2026ee4ae63875c291adf5025ab17112 to your computer and use it in GitHub Desktop.
Save victorkurauchi/2026ee4ae63875c291adf5025ab17112 to your computer and use it in GitHub Desktop.
Rxjs reducing to make serially http calls
// https://stackblitz.com/edit/rxjs-map-pipe-study-b2qoje
import { of, from } from 'rxjs';
import { map, flatMap, catchError } from 'rxjs/operators';
console.clear();
function fakeApiCall(value) {
console.log('Calling', value)
var lookupValues = { "a": 123, "b": 234, "c": 345, "d": 456 };
throw new Error('Hmmmm we are breaking things!')
// return { id: value, lookup: lookupValues[value] };
}
const totesToBeCreated = ['0014745', '0022'];
const sourceTotes = of(totesToBeCreated);
const example = sourceTotes.pipe(
flatMap((totesArray) => {
console.log('arr', totesArray);
return totesArray.reduce((previousObservable, toteNumber) => {
console.log('tote', toteNumber);
return previousObservable.pipe(
flatMap(response => {
console.log('response', response)
return of(fakeApiCall(toteNumber))
})
)
}, of(null))
}),
catchError((error) => {
console.log('Something went wrong!', error.message);
})
)
const subscribe = example.subscribe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment