Reputation: 83
Let's say I have an array of promises, itemsPromises
. Some of them will fail, half of them will probably succeed.
If I try to get the responses like so:
const itemsPromises = raw.map(item =>
axios({
method:'get',
url:`https://www.omdbapi.com/?apikey=${apikey}&i=${item.imdbID}`
})
)
const itemsResponses = await Promise.all(itemsPromises)
...I will need to wait a LONG time until the failing promises time out, eventually. I may get 5-6 successful reponses but won't have access to them untill ALL of the promises are resolved or rejected.
Can I convert this array of Promises to some iterable form of Observables, so that every time I get a successful response I can pass it on to my application and use it?
Upvotes: 4
Views: 595
Reputation: 11345
Merge operator lets you execute each http call simultaneously
Rx.Observable.merge(null,
raw.map(item =>
Rx.Observable.fromPromise(axios({
method: 'get',
url: `https://www.omdbapi.com/?apikey=b54e8554&i=${item.imdbID}`
})).catch(err=>Rx.Observable.of({err,item}))
)).subscribe()
Upvotes: 3