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

Add synchronous methods to the API #147

Closed
niklasl opened this issue Jul 24, 2012 · 1 comment
Closed

Add synchronous methods to the API #147

niklasl opened this issue Jul 24, 2012 · 1 comment

Comments

@niklasl
Copy link
Member

niklasl commented Jul 24, 2012

The JSON-LD API defines asynchronous methods with callbacks. I believe this is due to the need for asynchronous I/O when looking up external context references. While this is a common pattern in JS today (in browsers and node.js), it is not the only alternative. See e.g. the async keyword to XMLHttpRequest#open, fibers, continuations, etc. In many other languages (e.g. Python, Ruby), it is quite common to use synchronous I/O.

The relative merits of the two approaches can of course be debated ad nauseam (some find the async callback pattern superior, others call it "callback spaghetti"). And regardless of this, the async pattern isn't needed in the cases where no external I/O calls will happen anyway. That is, where there is no external context to be looked up, e.g. when doing compaction on expanded JSON-LD using an already loaded (or in code defined) custom context.

I believe the API should support both patterns. We could do this by either:

  1. Adding <methodName>Sync companions (e.g. expandSync and compactSync). This is how it's done in the core (common.js) I/O APIs in node.
  2. Detect if a callback is passed. If it is, use async and call it when done; if not, use sync and return the results. (As seen in some other APIs, e.g. in store creation of rdfstore-js.)
  3. Provide an explicit async option keyword to the call (similar to XMLHttpRequest#open).
@lanthaler
Copy link
Member

RESOLVED: The JSON-LD API method signatures across all languages are exactly the same. If a developer wants synchronous behavior, they MUST NOT add the callback parameter. Add a issue marker to the JSON-LD API spec stating that this functionality is at risk.

lanthaler added a commit that referenced this issue Aug 9, 2012
…n value

This addresses #147; I forgot to include it in 6c219c5.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants