Is there any undocumented complexities that anyone else has run into when using the importModule command with complex modules?
I have noticed that if there is any code that would be executed when a script is ran, aka. any code that is not in a function, that importModule will fail with the cryptic error of TypeError: undefined is not an object
I additionally had bugs with a function that would cascade through its promise chain without properly awaiting previous statements, but then would completely error out with the same type error if a catch statement was added to the end of the promise chain in an attempt to see what was happening.
I am currently attempting to make a very complex tool for interacting with the api for a ticketing tool that I use, and would love to be able to break my code into 5 or six different source files, both for readability as well as for reusability, as many of the api calls have highly redundant parts.
Im very tempted to categorize this as a bug, as the error that appears is opaque and difficult to understand.
Any assistance with this would be greatly appreciated.
The module documentation only describes examples around exports. If you have non-function based code to be executed, it isn’t explicitly described in the documentation as supporting that. Further, and somewhat more difficult to track down, Simon also discussed the general intent for the module import functionality in this thread, where the discussion indicates that the functionality is currently only intended for use with Scriptable specific libraries that you construct yourself.
It sounds like you might be attempting to utilise the functionality beyond what’s described in the above and that may be the source of your issues.
Perhaps in this regard, you just need to look at refactoring some of the library/module functionality to “play better with Scriptable”?
With regards to promises, well they can be tricky (I dread to think how many times I’ve hat to debug function calls with promises in them), but overall, the chains of promises should be respected and should work. I would suggest stripping things down to their simplest case and building it up to the point where you either have a promise-based structuring that works, or you have a clear example that @simonbs could use to reproduce, and then investigate the issue.
I think that my issue was actually a combination of a few different issues that were not clear at first.
Your suggestion to refactor really did help me with realizing how stuck in my was I was. Having most of my experience in python I was treating importModule as equivalent to import and that was tripping me up a whole bunch.