digitalbazaar / jsonld.js

A JSON-LD Processor and API implementation in JavaScript
https://json-ld.org/
Other
1.66k stars 195 forks source link

toRDF's documentLoader seems to require call to callback #400

Closed ericprud closed 4 years ago

ericprud commented 4 years ago

https://www.npmjs.com/package/jsonld#custom-document-loader suggests that a documentLoader can return a structure with a document property. When trying this with toRDF, I needed to call a callback with that structure of the event loop would drain without ever coming back from the asyn call. This is roughly what worked for me:

const quads = await Jsonld.toRDF(JSON.parse(jsonLdText), {documentLoader})

documentLoader: async (url, callback) => {
  if (url in Overrides) {
    const text = Overrides[url]
    callback(null, { contextUrl: null, document: JSON.parse(text), documentUrl: url })
  } else {
    return NodeDocumentLoader(url, callback)
  }
}

Should that be in the docs?

davidlehn commented 4 years ago

That's mixing async/await and callbacks. If that worked at all it's due to some older callback compatibility code that will eventually be removed. You should be able to do what's in the docs and just use async/await and promises style code. Do you have a short runnable example that fails? Also is this with the latest jsonld.js code?

A minor performance tweak, depending on your use case, might be to store the parsed JSON in Overrides so you don't have to re-parse it every load.

ericprud commented 4 years ago

My mistake, I wasn't using a new jsonld. It all works as expected with the latest major version.