0

Why is a sync xmlhttprequest deprecated ?

I would appreciate if the browser waits for the data instead of open it for further clicking without finalizing the data-query.

I would like to write a program with just one step after the other. Moreover the code should be properly structured and I wonder how I can realize this without the promise-hell.

Is there any sync alternative to xmlhttprequest which is not "deprecated" ?

Thanks Thomas

Thomas
  • 23
  • 2
  • Where do you see that xmlhttprequest is deprecated? Please give some more detail of what you are trying to accomplish, and provide some code so others can better help you. – BigHeadCreations Oct 11 '18 at 16:46
  • @BigHeadCreations it is. :) – epascarello Oct 11 '18 at 16:51
  • Why do you need to make an synchronous call? Why is it? Because it locks up the browser and can cause issue. A simple promise structure can be the same as a synchronous call. – epascarello Oct 11 '18 at 16:51
  • chrome brings the following warning: – Thomas Oct 11 '18 at 16:55
  • Test2.html:34 [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/. – Thomas Oct 11 '18 at 16:55
  • When I use a function which calls a function which calls a function a.s.o and I need an xmlhttprequest deeply inside the functional structure I would need to build up a complex promise system that the main level waits until the xmlhttprequest in the deepest level bring some result – Thomas Oct 11 '18 at 16:59
  • Well the only way around it is to make some complicated calls. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/await can be your friend here also. – epascarello Oct 11 '18 at 17:01
  • You answered your own question why it is deprecated, `[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org` – ponury-kostek Oct 11 '18 at 17:03

1 Answers1

1

Why is a sync xmlhttprequest deprecated ?

Because it blocks the Event Loop and provides bad user experience to your end users.

Basically it "freezes" the page while it's happening.

I would appreciate if the browser waits for the data

That is not how browsers work though. Fortunately with practice the way browsers do work start to make an awful lot of sense - enough for languages that have previously used mostly synchronous I/O like Python to add "promises" too.

I would like to write a program with just one step after the other.

You can return responses from asynchronous calls in JavaScript. Using async/await code looks pretty similar and readable. See the async function mdn page.

Moreover the code should be properly structured and I wonder how I can realize this without the promise-hell.

"promise-hell" refers to the fact once a function does I/O all other functions it calls that. I'm not convinced that's a bad thing or that there is anything wrong with

async function sequence() {
   let one = await fetch('/your-endpoint?someParam').then(x => x.json());
   // do something with one
   let param = one.param;
   let two = await fetch('/your-other?param=' + param).then(x => x.json());
   // do something with second call
}
sequence();

That is, async/await lets you write asynchronous code synchronously.

Don't expect to understand this all at once - it's ok it takes time.

Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504