2

This post method is using in angular2. In this ajax call , the post method is doesnt waiting for response. can any one say how to use observable for holding the asynchrouous in javascript until response receives .

this.http.post("http://jsonplaceholder.typicode.com/posts",body,options)
.map((res:Response) => res.json())
.subscribe(
    data => { console.log("data is",data)},
    err => console.error(err),
    () => console.log('done')
  );
Naveen Kumar
  • 167
  • 13
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Tamas Hegedus Oct 18 '16 at 08:40

2 Answers2

1

When the response arrives

data => { console.log("data is",data)},

is executed.

If you want other or more code to be executed when Data arrives, then add this code inside { } (instead or in addition to console.log(...))

That's how async execution works and there is no other way.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • i am not asking this @Gunter . In angular1, $q is used for stopping the execution of next line untile response receives .Like that i am asking how to use observables in angular2 – Naveen Kumar Oct 18 '16 at 08:30
  • I haven't used `$q` but I can't see what you would need it for with observables. What is the actual problem you try to solve? – Günter Zöchbauer Oct 18 '16 at 08:32
  • 1
    @NaveenKumar `$q` doesn't stop the execution of javascript, unless you are using `async-await`, which makes it *look like* the execution is stopped. – Tamas Hegedus Oct 18 '16 at 08:39
  • in my code http call doesnt wait for response .It continously executing next line .I want to execute code after the http call receives the response . – Naveen Kumar Oct 18 '16 at 08:44
  • That's exactly what my answer explains how to do it. If you add more code to your question, that demonstrates where this causes you troubles, I might be able to provide more concrete advice. – Günter Zöchbauer Oct 18 '16 at 08:46
0

Lets say this is your code:

let myData = null;
this.http.post("http://jsonplaceholder.typicode.com/posts",body,options)
.map((res:Response) => res.json())
.subscribe(
    data => {
        myData = data;
        console.log("data is",data);
    },
    err => console.error(err),
    () => console.log('done')
  );

console.log("Log the data again", myData);

So as you well know, the second logging will log null since the Observable probably hasn't retrieved a result yet. What you should do is simply put all the code that depends upon the data being ready inside the subscribe result handler, like so:

let myData = null;
this.http.post("http://jsonplaceholder.typicode.com/posts",body,options)
.map((res:Response) => res.json())
.subscribe(
    data => {
        myData = data;
        console.log("data is",data);
        console.log("Log the data again", myData);  // <--
    },
    err => console.error(err),
    () => console.log('done')
  );
Shai
  • 3,659
  • 2
  • 13
  • 26