AwokeKnowing AwokeKnowing - 6 months ago 14
Ajax Question

The "right" way to do synchronous HTTP request

You probably came here to chide me but this is a real use case.

In the world of online education, there are SCORM courses. I have to make old SCORM courses work on a site. SCORM courses are "web based" and run in a browser, but they expect to run in an iframe and they expect the parent to supply a GetValue method and a SetValue.

So these SCORM courses are doing things like parent.SetValue("score", "90") and moving on. That function is supposed to return "false" if there was any issue.

SCORM comes from the 90's, and in modern web we know we have to do callbacks/promises and http fails "often". You might think the solution is a SetValue that writes to local data and then tries and retries until it get's through, but the SCORM course typically is set up to only move to the next screen if the SetValue worked, so you shouldn't be letting the user advance unless the SetValue actually was saved on the server.

TL;DR

Assuming a syncronous request is a requirement, what is the right way to do it?

So far I know of

$.ajax({async:false ...
but now browsers warn about that and sound like they're going to just ignore your request to be synchronous. I am thinking maybe using websockets or web workers or something is the right way to do a syncronous request in modern programming. But I don't know how to make a request like that. And I am not allowed change the code of the SCORM courses (they are generated with various course-making tools)

To clarify, I have full control over the implementation of the SetValue function.

Will
$.ajax({async:false ...
work long term? (5-10 years)

NOTE: it is entirely acceptable in this use case to completely freeze the UI until the request either succeeds or fails. That's what the courses assume.

Answer

So far I know of $.ajax({async:false… but now browsers warn about that

This is the right way (if you're using jQuery), it sends a synchronous XMLHttpRequest. Just ignore the warning. It's a warning that you are using outdated technology, which you already know.

and sound like they're going to just ignore your request to be synchronous.

That's unlikely.

I am thinking maybe using websockets or web workers or something is the right way to do a syncronous request in modern programming.

No, websockets and web workers are always asynchronous, you can't use them to make your asynchronous request look synchronous (in fact there's nothing that lets you do this).

Will $.ajax({async:false… work long term? (5-10 years)

We cannot know (and SO is not a crystal ball). It might, especially in older browsers, or it might not. Browser vendors are reluctant to break compatibility of features that run the web, and synchronous requests still are needed from time to time. At some point, too few (important) web pages will use it (<1%, <1‰, whatever threshold they decide on) and browsers will finally be confident to remove it. At that point, your business will have realised to deprecate these outdated course-making tools.