-
Notifications
You must be signed in to change notification settings - Fork 2
Description
The fileGetContents function uses an asynchronous XMLHttpRequest to load files, but attempts to synchronously wait for the loading to occur. This does not work; JavaScript is not multi-threaded, so the asynchronous handler for the XHR's completion does not run until the currently running stack of functions completes. The XHR could be made synchronous with no timeout instead, or the XHR's timeout property could be used instead of blocking (note that the built-in timeout option and the synchronous request option are mutually exclusive in at least some browsers). The latter option would require the actions to be taken on the loaded files to occur asynchronously, triggered by the loading of the file in the onreadystatechange handler. If a basic callback-centric approach like that involves too much restructuring of the overall design, promises could be used instead (making the use of the loaded files asynchronous but retaining the same structure as synchronous code), although they require a polyfill in Internet Explorer. The downside of either asynchronous approach is that, since the output of the template transformation would be among the things that has to wait for the asynchronous load of the file, either way the interface would likely be affected.
#8 provides tests that demonstrate this issue.
In the absence of being able to load files through Tranjsform's built-in systems, I've been fiddling with a small wrapper that handles it: