1
    var x = "foo";
    this.http.get ("/myurl/a").subscribe (resp => console.log (x));

    x = "bar";
    this.http.get ("/myurl/b").subscribe (resp => console.log (x));

Output is a double "bar".

As far I understand asynchronous workflows that is explainable because the setting of value "bar" to variable x is happens normally before the finish of the 1st request.

Is there a way I can put a variable into the return of get (a Observable) with the value of it to the time of the call??

So I want to have "foo" and then "bar" for output.

chris01
  • 10,921
  • 9
  • 54
  • 93
  • 1
    Does this answer your question? [How do I return the response from an Observable/http/async call in angular?](https://stackoverflow.com/questions/43055706/how-do-i-return-the-response-from-an-observable-http-async-call-in-angular) – R. Richards Feb 20 '21 at 15:43

1 Answers1

1

arrow functions can solve this problem

var x = "foo";
this.http.get("/myurl/a").subscribe(((t) => (resp) => console.log(t))(x));

x = "bar";
this.http.get("/myurl/b").subscribe(((t) => (resp) => console.log(t))(x));