5 ms·
Hilarious, and a great breakdown of what's wrong with the code! But also, in an article calling out programming falsehoods, this snippet is a bit odd: > Even
by anamexis 6y ago
Hilarious, and a great breakdown of what's wrong with the code!
But also, in an article calling out programming falsehoods, this snippet is a bit odd:
> Even if apiService.sql returns a value synchronously (which I doubt), internally it have to open a connection to a database, make a query and send back the response, which (as you may have guessed) can’t be synchronous.
Sure it can, it's dead simple to write an API that accepts a database query and returns the result synchronously.
- ErikAugust 6y agoBut how would you call this API from the browser synchronously? I guess in theory there is a synchronous XHR call but I’ve never seen it used. That could be something you add to the problems.
- Izkata 6y agoIn the early days of ajax proliferation, I remember reading blog posts where people recommended synchronous XHR to keep your code linear, instead of trying to understand callbacks, which would make the code too complicated.
- bradleybuda 6y agoObviously crazy advice that the current generation of Javascript programmers was wise to disregard. /s
- idreyn 6y agoWell, yeah. You have one thread to work with in the browser, and if you tie it up with a synchronous XHR your application freezes until the request finishes.
- cortesoft 6y agoSynchronous XHR calls were not that uncommon back in the day.
- baron816 6y agoYikes
- imtringued 6y agoBy googling "jquery synchronous ajax". Here is the first result: https://stackoverflow.com/a/6685294 https://stackoverflow.com/a/6685294 Although I don't recommend doing this it's hardly as difficult as your comment implies.
- ErikAugust 6y agoNot hard, but certainly wrong and deprecated as far as I can tell.
- manquer 6y agoDeprecated Only in the main Thread , using in web worker is a perfectly valid and acceptable use
- sreekotay 6y agoOn its way out... but.... synchronous XHR still works. https://xhr.spec.whatwg.org/ https://xhr.spec.whatwg.org/