0

Because of how things are transmitted across a network, some files will inevitably reach later than others. Lets say I have two js files a.js and b.js, and "a" relies on a certain object in "b"; do I need to explicitly import "b's" code into "a" so that "a" wont try and do anything before it can reliably import the necessary object from "b" and cause an error?

Jaigus
  • 1,422
  • 1
  • 16
  • 31
  • 1
    Possible duplicate of https://stackoverflow.com/questions/1293367/how-to-detect-if-javascript-files-are-loaded – TidyDev Oct 18 '17 at 19:40
  • You may need dependency injection - https://stackoverflow.com/questions/20058391/javascript-dependency-injection – colecmc Oct 18 '17 at 19:42

1 Answers1

0

Javascript files are loaded synchronously, in the order the are declared in the HTML. Therefore if 'B' is always declared before 'A', the dependencies script A requires should always be available.

There are other methods to load additional JS files in an asynchronous manner, which could cause errors if certain dependencies have not fully loaded.

You could also implement loading of a JS file within another, thus giving you full control of how and when your files will load. Reference here

NSTuttle
  • 3,237
  • 2
  • 17
  • 26