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;








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:



  1. Query an API with axios

  2. Create an observable using the value resolved by axios

  3. Pipe that observable to a map operator that returns an observable

  4. Convert return observable to promise

  5. Resolve promise to expose data.









share|improve this question
























  • 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

















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:



  1. Query an API with axios

  2. Create an observable using the value resolved by axios

  3. Pipe that observable to a map operator that returns an observable

  4. Convert return observable to promise

  5. Resolve promise to expose data.









share|improve this question
























  • 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













0












0








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:



  1. Query an API with axios

  2. Create an observable using the value resolved by axios

  3. Pipe that observable to a map operator that returns an observable

  4. Convert return observable to promise

  5. Resolve promise to expose data.









share|improve this question














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:



  1. Query an API with axios

  2. Create an observable using the value resolved by axios

  3. Pipe that observable to a map operator that returns an observable

  4. Convert return observable to promise

  5. Resolve promise to expose data.






javascript typescript rxjs






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















0
















// 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





share|improve this answer



























    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
    );



    );














    draft saved

    draft discarded
















    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









    0
















    // 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





    share|improve this answer





























      0
















      // 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





      share|improve this answer



























        0














        0










        0









        // 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





        share|improve this answer













        // 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






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 29 at 1:50









        Ayinla AbdulsalamAyinla Abdulsalam

        113 bronze badges




        113 bronze badges

































            draft saved

            draft discarded















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript