Loading a template via assets.load does not load its associated assets before calling callback

I have a large number of templates that I need to load at runtime to cut down on load times. My code to ensure that I have all assets before using them looks like this:

Global.prototype.ensureAssetsLoaded = function(toLoad, callback, callbackScope) {
    let assetCount = toLoad.length;
    let assetsLoaded = 0;
    let onLoad = function(asset, assetCategory, assetCode) {
        console.log('loaded '+assetCategory+'.'+assetCode);
        assetsLoaded++;
        
        if (assetsLoaded >= assetCount) {
            callback.call(callbackScope);
        }
    };
    
    for (let i=0; i<assetCount; i++) {
        console.log('loading: '+toLoad[i]);
        
        let splitID = toLoad[i].split('.');
        let category = splitID[0];
        let code = splitID[1];
        
        let asset = pc.app.assetMap[category][code];
        asset.ready(function (loadedAsset) {
            pc.app.assetMap[category][code] = loadedAsset;
            onLoad(loadedAsset, category, code);
        });
        pc.app.assets.load(asset);
    }
};

Basically we just get a list of assets, load them via assets.load, and count the number of times onLoad gets called. When we’ve loaded enough assets, we fire off a callback to continue the program.

This is mostly working, but when loading a template onLoad will be called after the template itself is loaded but before any glb references inside of it are loaded. This means that everything I do immediately after will fail, because all meshInstances are still undefined. I can see in the browser console that the glb files are actually being loaded, and if I repeatedly call this function it will successfully load exactly 1 more template every time.

Loading a template asset will not load the assets referenced inside the template. Those will have to be loaded separately.