Closed Octalbyte closed 3 years ago
Just added synchronous reading method.
Added readdirSync
Usage:
let files = readdirSync("foo") // throws error if directory does not exist
let mydata = readFileSync("foo.bar")
writeFileSync("baz.txt", myBuffer) // should return null
Ready for review.
@J-P-S-O hi! did you test it? In general adding sync apis are hard as most things in Node are async, such as streams, network etc (which is why we haven't added any)
Well, the methods aren't very inovating, they just create the stream with all the callbacks and error handling hidden to the user.
They depend on the async methods.
@J-P-S-O yes, but you cannot return sync from a stream basically. If you try calling your readSync method here you'll see it just returns undefined as the sync function returns immediately.
I'm performing the tests.
I'll try to solve it.
Hey @J-P-S-O,
Appreciate the work you're putting into this, but the fundamental issue is that all underlying operations inside of Hyperdrive are async. The way you're approaching the sync methods really isn't possible. On the read side, the data isn't available sync, so you can never return sync. On the write side, if you return sync you're going to be suppressing errors or throwing them in the wrong place.
In this way, Hyperdrive isn't analagous to Node's fs
module, because we can't do blocking IO.
I think to accomplish what you're looking for, you can use the Promises interface on drive.promises
, so you can await drive.promises.readFile(...)
.
Hi @andrewosh, thanks for the tip!
Ah I'm sorry @J-P-S-O think there was a misunderstanding: I don't think we should have sync methods on Hyperdrive at all.
What I meant in my previous comment was that the usage you're looking for (calling Hyperdrive methods without dealing with callbacks) might already be covered by the Promises interface (drive.promises
).
Ok @andrewosh , now I understood it. I'm closing this Pull request.
This pull is in progress. Just added synchronous
writeFileSync