2 ms·
This is the opposite in JavaScript (and similar single-threaded languages) where sync code is atomic and normally can't do I/O.
by skybrian 2y ago
This is the opposite in JavaScript (and similar single-threaded languages) where sync code is atomic and normally can't do I/O.
- akira2501 2y agonode.js has a whole plethora of synchronous IO methods. they're actually really nice. or have I misunderstood your meaning?
- maronato 2y agoNode uses libuv[1] for all its IO operations[2]. The sync versions still use libuv, but block the main thread until they receive the result. [1] https://libuv.org https://libuv.org [2] https://nodejs.org/en/learn/asynchronous-work/overview-of-blocking-vs-non-blocking https://nodejs.org/en/learn/asynchronous-work/overview-of-bl...
- throwitaway1123 2y agoYou can perform file I/O in JavaScript synchronously. The localStorage API in the browser is synchronous (and in Node via the --experimental-webstorage option), and of course requiring a CommonJS module is also synchronous (and there are many other sync filesystem APIs in Node as a sibling comment pointed out). You just can't perform network I/O synchronously. Although a network attached file system allows for both network and file I/O technically, but that's a really pedantic point.
- Izkata 2y ago> You just can't perform network I/O synchronously. Sure you can, you just shouldn't ever do it because it blocks the UI: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest_API/Synchronous_and_Asynchronous_Requests#synchronous_request https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequ...
- throwitaway1123 2y agoYeah I should've said there's no Node API for making synchronous HTTP requests (unless you count executing a child process synchronously). Even the older http.request API used in Node prior to the introduction of fetch is async and accepted a callback. Browsers have all sorts of deprecated foot guns though (like the synchronous mode of XMLHttpRequest).