Piping Observables and Returning a Promiseevent.preventDefault() vs. return falseWhy does ++[[]][+[]]+[+[]] return the string “10”?How do I return the response from an asynchronous call?Hot and shared Observable from an EventEmitterAngular2 Observable and PromiseWhat is the difference between Promises and Observables?BehaviorSubject vs Observable?Is this method of creating a HTTP Service in Angular correct?Catching errors in Promise, Observable.fromPromise() and subscribtionAngular - receive and return Observable<T> response in Http.post
How to bring home documents from work?
Top off gas with old oil, is that bad?
Duck, duck, gone!
How to compare integers in TeX?
Impossible violin chord, how to fix this?
Diminished data rate with logic output optoisolator
How to add the real hostname in the beginning of Linux cli command
Sci-fi movie with one survivor and an organism(?) recreating his memories
Is it good to engage in exceptional cases where it is permissible to do a typically forbidden action to which one has a taivah for
As a team leader is it appropriate to bring in fundraiser candy?
How important is knowledge of trig identities for use in Calculus
Detail vs. filler
Why most footers have a background color has a divider of section?
How to study endgames?
Can an energy drink or chocolate before an exam be useful ? What sort of other edible goods be helpful?
Can I pay some of the cost of an activated ability lots of times to get more out of the effect?
Garage door sticks on a bolt
How is the Apple Watch ECG disabled in certain countries?
Is there an in-universe explanation of how Frodo's arrival in Valinor was recorded in the Red Book?
Implementation of a Thread Pool in C++
Knights and Knaves: What does C say?
grounded outlets and code compliance
Did Tolkien ever write about a Heaven or Hell for Men?
I transpose the source code, you transpose the input!
Piping Observables and Returning a Promise
event.preventDefault() vs. return falseWhy does ++[[]][+[]]+[+[]] return the string “10”?How do I return the response from an asynchronous call?Hot and shared Observable from an EventEmitterAngular2 Observable and PromiseWhat is the difference between Promises and Observables?BehaviorSubject vs Observable?Is this method of creating a HTTP Service in Angular correct?Catching errors in Promise, Observable.fromPromise() and subscribtionAngular - receive and return Observable<T> response in Http.post
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
Why does the code below return a promise
that contains an observable
?
const queryApi = (request: AxiosRequestConfig): Observable<ParsedData> =>
return new Observable(observer =>
axios(request).then(response =>
observer.next(response);
observer.complete();
).catch(error =>
observer.error(error);
)
).pipe(map(value =>
const parser = new Parser(name);
const response = value as AxiosResponse;
return parser.parse(response.data);
));
const sourcePromise: Promise<ParsedData> = queryApi(request, 1).toPromise();
await sourcePromise.then(source$ =>
source$.subscribe((value: ParsedData) => console.log(JSON.stringify(value, null, 2)));
).catch(error => console.log(error));
I was expecting the code to:
- Query an API with
axios
- Create an observable using the value resolved by
axios
- Pipe that observable to a
map
operator that returns anobservable
- Convert return
observable
topromise
- Resolve
promise
to expose data.
javascript typescript rxjs
add a comment
|
Why does the code below return a promise
that contains an observable
?
const queryApi = (request: AxiosRequestConfig): Observable<ParsedData> =>
return new Observable(observer =>
axios(request).then(response =>
observer.next(response);
observer.complete();
).catch(error =>
observer.error(error);
)
).pipe(map(value =>
const parser = new Parser(name);
const response = value as AxiosResponse;
return parser.parse(response.data);
));
const sourcePromise: Promise<ParsedData> = queryApi(request, 1).toPromise();
await sourcePromise.then(source$ =>
source$.subscribe((value: ParsedData) => console.log(JSON.stringify(value, null, 2)));
).catch(error => console.log(error));
I was expecting the code to:
- Query an API with
axios
- Create an observable using the value resolved by
axios
- Pipe that observable to a
map
operator that returns anobservable
- Convert return
observable
topromise
- Resolve
promise
to expose data.
javascript typescript rxjs
i tihnk it might have something to do with not being completed but if you want to get a promise anyway why not jsut return a promise in the first place
– jonathan Heindl
Mar 28 at 20:26
I haven't used typescript, but it looks like you aren't calling the promise, just referencing it.sourcePromise.then
->sourcePromise().then
– Nathan
Mar 28 at 20:29
Are you saying thatparser.parse
returns an observable?
– Bergi
Mar 28 at 20:54
Why are you using observables at all here? You're turning a promise into an observable, then turning that back into a promise. Just use promises directly to make your code much simpler.
– Bergi
Mar 28 at 20:55
Where exactly in that code does it "return a promise that contains an observable"?
– Bergi
Mar 28 at 20:56
add a comment
|
Why does the code below return a promise
that contains an observable
?
const queryApi = (request: AxiosRequestConfig): Observable<ParsedData> =>
return new Observable(observer =>
axios(request).then(response =>
observer.next(response);
observer.complete();
).catch(error =>
observer.error(error);
)
).pipe(map(value =>
const parser = new Parser(name);
const response = value as AxiosResponse;
return parser.parse(response.data);
));
const sourcePromise: Promise<ParsedData> = queryApi(request, 1).toPromise();
await sourcePromise.then(source$ =>
source$.subscribe((value: ParsedData) => console.log(JSON.stringify(value, null, 2)));
).catch(error => console.log(error));
I was expecting the code to:
- Query an API with
axios
- Create an observable using the value resolved by
axios
- Pipe that observable to a
map
operator that returns anobservable
- Convert return
observable
topromise
- Resolve
promise
to expose data.
javascript typescript rxjs
Why does the code below return a promise
that contains an observable
?
const queryApi = (request: AxiosRequestConfig): Observable<ParsedData> =>
return new Observable(observer =>
axios(request).then(response =>
observer.next(response);
observer.complete();
).catch(error =>
observer.error(error);
)
).pipe(map(value =>
const parser = new Parser(name);
const response = value as AxiosResponse;
return parser.parse(response.data);
));
const sourcePromise: Promise<ParsedData> = queryApi(request, 1).toPromise();
await sourcePromise.then(source$ =>
source$.subscribe((value: ParsedData) => console.log(JSON.stringify(value, null, 2)));
).catch(error => console.log(error));
I was expecting the code to:
- Query an API with
axios
- Create an observable using the value resolved by
axios
- Pipe that observable to a
map
operator that returns anobservable
- Convert return
observable
topromise
- Resolve
promise
to expose data.
javascript typescript rxjs
javascript typescript rxjs
asked Mar 28 at 20:18
AriAri
1,6357 gold badges27 silver badges45 bronze badges
1,6357 gold badges27 silver badges45 bronze badges
i tihnk it might have something to do with not being completed but if you want to get a promise anyway why not jsut return a promise in the first place
– jonathan Heindl
Mar 28 at 20:26
I haven't used typescript, but it looks like you aren't calling the promise, just referencing it.sourcePromise.then
->sourcePromise().then
– Nathan
Mar 28 at 20:29
Are you saying thatparser.parse
returns an observable?
– Bergi
Mar 28 at 20:54
Why are you using observables at all here? You're turning a promise into an observable, then turning that back into a promise. Just use promises directly to make your code much simpler.
– Bergi
Mar 28 at 20:55
Where exactly in that code does it "return a promise that contains an observable"?
– Bergi
Mar 28 at 20:56
add a comment
|
i tihnk it might have something to do with not being completed but if you want to get a promise anyway why not jsut return a promise in the first place
– jonathan Heindl
Mar 28 at 20:26
I haven't used typescript, but it looks like you aren't calling the promise, just referencing it.sourcePromise.then
->sourcePromise().then
– Nathan
Mar 28 at 20:29
Are you saying thatparser.parse
returns an observable?
– Bergi
Mar 28 at 20:54
Why are you using observables at all here? You're turning a promise into an observable, then turning that back into a promise. Just use promises directly to make your code much simpler.
– Bergi
Mar 28 at 20:55
Where exactly in that code does it "return a promise that contains an observable"?
– Bergi
Mar 28 at 20:56
i tihnk it might have something to do with not being completed but if you want to get a promise anyway why not jsut return a promise in the first place
– jonathan Heindl
Mar 28 at 20:26
i tihnk it might have something to do with not being completed but if you want to get a promise anyway why not jsut return a promise in the first place
– jonathan Heindl
Mar 28 at 20:26
I haven't used typescript, but it looks like you aren't calling the promise, just referencing it.
sourcePromise.then
-> sourcePromise().then
– Nathan
Mar 28 at 20:29
I haven't used typescript, but it looks like you aren't calling the promise, just referencing it.
sourcePromise.then
-> sourcePromise().then
– Nathan
Mar 28 at 20:29
Are you saying that
parser.parse
returns an observable?– Bergi
Mar 28 at 20:54
Are you saying that
parser.parse
returns an observable?– Bergi
Mar 28 at 20:54
Why are you using observables at all here? You're turning a promise into an observable, then turning that back into a promise. Just use promises directly to make your code much simpler.
– Bergi
Mar 28 at 20:55
Why are you using observables at all here? You're turning a promise into an observable, then turning that back into a promise. Just use promises directly to make your code much simpler.
– Bergi
Mar 28 at 20:55
Where exactly in that code does it "return a promise that contains an observable"?
– Bergi
Mar 28 at 20:56
Where exactly in that code does it "return a promise that contains an observable"?
– Bergi
Mar 28 at 20:56
add a comment
|
1 Answer
1
active
oldest
votes
// Using rxjs 6
// This should solve your problem.
import axios from 'axios';
import from from 'rxjs';
import map from 'rxjs/operators';
const queryApi = request => from(axios(request)).pipe(map(res => res.data));
//Make sure to subscribe to the observable
add a comment
|
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406223%2fpiping-observables-and-returning-a-promise%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
// Using rxjs 6
// This should solve your problem.
import axios from 'axios';
import from from 'rxjs';
import map from 'rxjs/operators';
const queryApi = request => from(axios(request)).pipe(map(res => res.data));
//Make sure to subscribe to the observable
add a comment
|
// Using rxjs 6
// This should solve your problem.
import axios from 'axios';
import from from 'rxjs';
import map from 'rxjs/operators';
const queryApi = request => from(axios(request)).pipe(map(res => res.data));
//Make sure to subscribe to the observable
add a comment
|
// Using rxjs 6
// This should solve your problem.
import axios from 'axios';
import from from 'rxjs';
import map from 'rxjs/operators';
const queryApi = request => from(axios(request)).pipe(map(res => res.data));
//Make sure to subscribe to the observable
// Using rxjs 6
// This should solve your problem.
import axios from 'axios';
import from from 'rxjs';
import map from 'rxjs/operators';
const queryApi = request => from(axios(request)).pipe(map(res => res.data));
//Make sure to subscribe to the observable
answered Mar 29 at 1:50
Ayinla AbdulsalamAyinla Abdulsalam
113 bronze badges
113 bronze badges
add a comment
|
add a comment
|
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406223%2fpiping-observables-and-returning-a-promise%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
i tihnk it might have something to do with not being completed but if you want to get a promise anyway why not jsut return a promise in the first place
– jonathan Heindl
Mar 28 at 20:26
I haven't used typescript, but it looks like you aren't calling the promise, just referencing it.
sourcePromise.then
->sourcePromise().then
– Nathan
Mar 28 at 20:29
Are you saying that
parser.parse
returns an observable?– Bergi
Mar 28 at 20:54
Why are you using observables at all here? You're turning a promise into an observable, then turning that back into a promise. Just use promises directly to make your code much simpler.
– Bergi
Mar 28 at 20:55
Where exactly in that code does it "return a promise that contains an observable"?
– Bergi
Mar 28 at 20:56