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
(node:22420) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: Cannot read property 'domBuilder' of undefined
(node:22420) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
And that is because the "this" object has changed from where the parser was created and from the moment the promise is being evaluated. The error is on the third line below (in dom-parser.js)
DOMParser.prototype.parseFromString=function(source,mimeType){varoptions=this.options;varsax=newXMLReader();vardomBuilder=options.domBuilder||newDOMHandler();//contentHandler and LexicalHandler
The options variable is going to be undefined (because the "this" object referring to the context being executed in the current "thenable" block does not have such a property. Then when we try to create domBuilder, it inevitably breaks.
One solution is to lazy evaluate construction of the parser till the last moment (not always possible)
constDOMParser=require('xmldom').DOMParser;Promise.resolve(true).then(readSomeFile).then((x)=>{return(newDOMParser()).parseFromString(x)})// meh, ok sometimes
Or, if you must instantiate the parser outside of the thenable blocks, to lazily and explicitly bind to
it
I would expect the behavior you experience. It's common to need to explicitly bind a method to object when passing the method as a function. Rather than patching this code, I would expect a bound function to passed into the promise:
DOMParser fails to bind to the proper "this" object when a DOMParser object is created outside of a promise, but used within a "thenable" block.
For instance.
Will cause the following error to occur.
And that is because the "this" object has changed from where the parser was created and from the moment the promise is being evaluated. The error is on the third line below (in dom-parser.js)
The options variable is going to be undefined (because the "this" object referring to the context being executed in the current "thenable" block does not have such a property. Then when we try to create domBuilder, it inevitably breaks.
it
the constructor from this:
To this:
I've tested all changes, and my preference would be the last one.
Until (and if) such a change is incorporated, the workaround I'm using is number one and two above, depending on context.
The text was updated successfully, but these errors were encountered: