You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
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.
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.)
Provide an explicit async option keyword to the call (similar to XMLHttpRequest#open).
The text was updated successfully, but these errors were encountered:
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.
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:
<methodName>Sync
companions (e.g.expandSync
andcompactSync
). This is how it's done in the core (common.js) I/O APIs in node.async
option keyword to the call (similar toXMLHttpRequest#open
).The text was updated successfully, but these errors were encountered: