4 ms·
Code formatting is indeed minor here compared to the rest, but you missed the one that popped out at me: The opening curly braces are sometimes on the same lin
by Izkata 6y ago
Code formatting is indeed minor here compared to the rest, but you missed the one that popped out at me: The opening curly braces are sometimes on the same line as the condition, sometimes on the following line.
That said,
> I’m absolutely sure that the code above is fake.
> That’s the first time that I see a synchronous SQL query:
var accounts = apiService.sql(
"SELECT * FROM users"
);
Pretty sure you meant synchronous ajax/web request rather than sql query. This could be old code, because it totally used the be a thing: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Synchronous_and_Asynchronous_Requests#Synchronous_request https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequ...
Edit: Actually hell, it still works, at least in Firefox. It just also logs the deprecation warning mentioned on that page. I thought this was already removed.
- tom_ 6y agoYou'd have to see all of the code to be sure, but the braces here are on the same line as the statement that opened the scope, except one case where the opening line was split. You don't have to approve, but there may at least be a reason.
- lolc 6y agoFunny I almost commented that you must be mistaken because it would be positively insane to support synchronous requests. Wow, that is insane. In the end, synchronization can be achieved by busy waiting. So the implementation of apiService.sql() could just rely on that even if the browser API didn't support it. The code still looks fake to me. Because you have to know quite a bit to create a combination of fails like that. Natural bad code I've seen is less purposefully structured. But maybe I just haven't been exposed to this type.
- Izkata 6y ago> In the end, synchronization can be achieved by busy waiting. So the implementation of apiService.sql() could just rely on that even if the browser API didn't support it. I haven't tested this, but I don't think it can. The result callback doesn't have a chance to run if there's a busy loop - javascript is single-threaded, and callbacks only fire when idle (think cooperative multitasking, not preemptive multitasking). Webworkers may get around this, but I've not used them and am not sure of the details on how they work. A quick look at a guide indicates they still rely on event callbacks in the main thread, so if my thought above is right, it looks like they wouldn't work either.
- lolc 6y agoIt's true! I tested it because I thought it would be possible to just wait for readyState == 4 in a loop. My assumption was that XMLHttpRequest runs in another thread once you do send() on it. And indeed, the request starts immediately. But the instance of XMLHttpRequest doesn't get updated until control is yielded. So you can't get the goods. All considered, that is a good thing :-)
- CodesInChaos 6y agoA couple of years ago slack used synchronous requests to fetch the history when scrolling up on the main chat window. Page/browse freezing included. I think it even used a full CPU core spinning instead of blocking on IO.