9 ms·
I think JS devs usually use async/await for network calls and not really for computation or file access. Those type of applications are better served by other l
by drevil-v2 7y ago
I think JS devs usually use async/await for network calls and not really for computation or file access. Those type of applications are better served by other languages.
A deadlock in the problem space that JavaScript operates in would be a rarity I feel.
- jdm2212 7y agoJS is single-threaded right so (...I think...) deadlocks are actually impossible. Although I guess you can still get stuck if two pieces of code are waiting on each other to satisfy some condition (and trading control of the sole thread) without using explicit locking.
- foota 7y agoYeah, nothing about JavaScript being single threaded implies any resistance to deadlock.
- jdm2212 7y agoIt's still much harder to deadlock single-threaded code than multi-threaded code because single-threading eliminates the need for most locking, which in turn eliminates most opportunities to cause deadlocks.
- matharmin 7y agoDeadlocks can happen in any kind of concurrent system with locks. As soon as you start building & using locks with Promises, this is quite easy to happen. That said, the need for locks in JavaScript is much less than in a typical multi-threaded language, so it's a lot easier to keep track of and avoid deadlocks, if you're even using locks at all.
- ChristianBundy 7y agoI've been using async/await especially for file access. const fs = require('fs').promises const path = require('path') const filePath = path.join(__dirname, 'package.json') const fileContents = await fs.readFile(filePath, 'utf8') console.log(fileContents)
- drevil-v2 7y agoWell I personally would use a synchronous method rather than async for something like that where the next code path depends on the return data; if you can't get the file contents then you want to throw an error straight away.
- galaxyLogic 7y agoThat would seem to make sense, using readFileSync() instead. I assume it would really do much the same thing as the await example. Is there any difference? If they do the same thing, then what's the benefit of async/await? I guess it's that you can now write your own "fs.readFileSync()" or something like that in JavaScript when needed.
- jeswin 7y agofs.readFileSync() blocks the event loop preventing any other code from being run or other events/requests from being processed. Await yields control back so that other requests can be processed while the file is being read.
- galaxyLogic 7y agoAh I see. Genius. So is there ever a use-case for NOT using the Async/Await -version of readFile()? Is fsReadSync() now just legacy which we don't need any more?
- ihuman 7y agoIs there an advantage to using "await fs.readFile" over "fs.readFileSync"?