0

What if I want to ensure that an HTTP call is synchronous because until the data is retrieved, no other operations must take place?

Why is there no option for writing a synchronous call in Angular 2 or higher versions?

rahs
  • 1,759
  • 2
  • 15
  • 31
  • 1
    Freezing the UI for an http call to end is very bad for user experience. It is better to make the user feel like everything is already loaded even if not. – ibenjelloun Apr 27 '18 at 13:49

1 Answers1

4

Because this is an anti-pattern.

Http-Calls might always fail (as anything going over a network - this is out of your reach as a programmer) and if they fail your whole application fails, providing no fallback or at least a visual notification for the user that something is wrong.

And even when they don't fail they might take a long time during which your application should not appear frozen, so the user doesn't close it or throws the phone against the wall.

If you really want to make things synchronous, you can use several RXJS operators for that; see:

How can I make one RxJS Observable sequence wait for another to complete before emitting?

Phil
  • 7,065
  • 8
  • 49
  • 91
  • Thanks! I don't need to make an observable wait before another observable's execution starts. I need it to wait before other non-observable code executes. – rahs Apr 27 '18 at 14:06
  • If you want to delay an observable until synchronous code does something you can do: `private $somethingHasBeenDone = new Subject();` and `someMethod() { $somethingHasBeenDone.next(); }` then you can wait for $somethingHasBeenDone to be triggered and handle it like in the linked post. – Phil Apr 27 '18 at 14:11