synchronous variant of jsonata #758
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi 👋
We're using jsonata to allow our customers to write custom matchers for the state of an object. As it stands right now, this means the place where we call
jsonata(customerThing.expression).evaluate(ourData)
we have to addawait
. Which means that function has to become async, and we ideally don't want that. We don't want to enable any custom functions, and certainly not async ones. So we'd prefer to have a synchronous version of jsonata. And looking at the code, I noticed that almost none of it needs to be asynchronous for any reason other than it might call some asynchronous user-defined function.So, this PR uses a really dumb script to create a whole separate submodule of the package (under
./sync
) which:async
andawait
with/* async */
and/* await */
Promise.all(...)
withArray.from(...)
(pointless calling Array.from on something that's already an array, but avoids complicated syntax modificationif (isPromise(result)) { ... }
withif (isPromise(result)) throw new Error('no promises pls')
It also copies over all tests, modifying them to use the sync version of jsonata instead:
async-function.js
tests which are specifically for time-based and user-defined async functionNot sure if you'll want to accept and maintain this - the implementation right now is a hack. But it seems pretty valuable to provide an API that doesn't require calling from an async function. So, opening a PR if for no other reason than as a PSA - it can be used right now, at least from node, by adding
"jsonata": "github:mmkal/jsonata#syncify.js"
to package.json.Note: a library like quansync might be a good option for supporting both sync and async without having to duplicate the code.