Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
167
rated 0 times [  169] [ 2]  / answers: 1 / hits: 22665  / 9 Years ago, wed, december 9, 2015, 12:00:00

I have an Observable in which I consume another observable, but the 2nd Observable I can't get to resolve. Here is the code:



return Observable.fromPromise(axios(config))
.map(res => {
return {
accessToken: res.data.access_token,
refreshToken: res.data.refresh_token
}
})
.map(res => {
return {
me: getMe(res.accessToken),
accessToken: res.accessToken,
refreshToken: res.refreshToken
}
})

function getMe(accessToken) {
return Observable.fromPromise(axios.get({
url: 'https://api.spotify.com/v1/me',
}));
}


The getMe function returns an Observable, but it is never resolved. I have tried to add a flatMap and a concat, but it still isn't resolved. How do I get the getMe to resolve?


More From » rxjs

 Answers
4

Did you try the following (Also untested):



function getMe(accessToken) {
return Rx.Observable.fromPromise(axios.get({
url: 'https://api.spotify.com/v1/me',
}));
}

Rx.Observable.fromPromise(axios(config))
.map((res) => {
return {
accessToken: res.data.access_token,
refreshToken: res.data.refresh_token
}
})
.flatMap((res) => {
return getMe(res.accessToken).map((res2) => {
res.me = res2;
return res;
}
})
.subscribe((data) => console.log(data));


As mentioned in the above post, flatMap returns an observable. map is subsequently used to merge res with the result res2 returned from the second promise.



Also note that fromPromise is a cold observable. This means that you must have a subscription to initiate things. In your case, I presume you already have something like this:



someFunction = () => {
return Rx.Observable.fromPromise(axios(config))
...
...
}

someFunction.subscribe((data) => console.log(data));

[#64114] Monday, December 7, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jaliyahcynthiac

Total Points: 91
Total Questions: 94
Total Answers: 119

Location: Vanuatu
Member since Wed, Oct 14, 2020
4 Years ago
;