Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileSystemAccess.prototype.readText has an async interface, but is synchronous #537

Closed
moll opened this issue Aug 1, 2015 · 8 comments
Closed
Milestone

Comments

@moll
Copy link

moll commented Aug 1, 2015

Hey,

I was going through the source code and stumbled upon something pretty weird.

new FileSystemAccess().readText(name, onSuccess, onError)

That's an async interface, but according to https://github.com/NativeScript/NativeScript/blob/5e5d75c09d8a3a49bbf6de5bfa53bc1736e99de4/apps/tests/file-system-access-tests/file-system-access-tests.ts and its use in builder.ts, it works synchronously. That's zalgo right there. A sync interface is handy, but the weird interface is, well, weird. It should return the content of the file on success and throw on error.

Cheers.

@enchev
Copy link
Contributor

enchev commented Aug 3, 2015

Indeed file access is synchronous for now however once we have support for background workers/threads we will improve this.

@moll
Copy link
Author

moll commented Aug 3, 2015

Till then, the API should be renamed to, e.g., Node style's readFileSync and skip the callback faux pas. ;-)

@atanasovg
Copy link
Contributor

I think we should emphasize on the usage of the file-system module rather than the file-system-access one. The first one should be the promoted/suggested way for accessing the file system, while the second one should not be publicly accessed. Either way, we need to extend the API with the sync suffix which semantically describes the operation.

@enchev enchev closed this as completed Aug 11, 2015
@enchev enchev added the done label Aug 11, 2015
@enchev enchev added this to the 1.3.0 milestone Aug 11, 2015
@moll
Copy link
Author

moll commented Aug 11, 2015

As per #554 (comment) this might just not yet be finished.

@atanasovg
Copy link
Contributor

The onError parameter is optional and it actually provides another means to try-catch your code. Is try-catch the preferred (or kosher) way to do error handling?

@moll
Copy link
Author

moll commented Aug 11, 2015

Hey. You're right, it's not really the best solution for a synchronous API. It reinvents what try-catch is there for. It's also not clear to read as why should a synchronous API take a callback. I haven't checked how the async versions now behave (perhaps there still aren't any).

Anywho, synchronous APIs in JavaScript should stick to returning a value or throwing an exception. Theoretically that could be implemented in union types to forgo exceptions, but I wouldn't. Most other [sync] APIs are exception based here. https://www.joyent.com/developers/node/design/errors is a decent enough reference to start thinking about error design.

Once you get to async interfaces, I wouldn't invent the wheel either. Promises or Node.js style continuation-passing with the same error API and you're good to go. ;-) Feel free to give me a call someday if you want to chat about APIs.

@atanasovg
Copy link
Contributor

@moll thanks for the heads-up :) I tend to agree with you and the suggested way for error handling. Truth is the file-system module was one of the first modules in the early days of {N} and we weren't sure how exactly to write asynchronous APIs - Node vs. Cordova vs. Promises. We started the Cordova way and obviously we've made some mixtures of both synchronous and asynchronous approaches.

@enchev enchev added the bug label Sep 16, 2015
@lock
Copy link

lock bot commented Aug 31, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked and limited conversation to collaborators Aug 31, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants