Last active
March 4, 2020 09:45
-
-
Save victorkurauchi/2026ee4ae63875c291adf5025ab17112 to your computer and use it in GitHub Desktop.
Rxjs reducing to make serially http calls
This file contains hidden or 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
// 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