You don’t need to make two STLLoader instances. Just reuse the same one. Then, to be sure you have both models available in time, use the callbacks on the manager instead of on the loader.
Yes, but why? It will potentially make the overall load time longer, so there must be a good reason to do so. When is loading order important? Can’t all the benefits be achieved by calling the init functions in the desired order from the loading manager callback?
Edit: If it is desired to get some models visible before all have been loaded, my solution will not apply, as it involves waiting until all are loaded before using any of them.
here’s a piece of code I wrote for my needs, feel free to use it as well if you wish.
/**
*
* @param {Array<function(*):Promise>} factories
* @param {Promise} [head] chain head, promise to be resolved before rest of the links
* @param {[]} [parameters] parameters are passed into each factory
* @returns {Promise}
*/
function buildPromiseChain(
{
factories,
head = Promise.resolve(),
parameters = []
}
) {
const numFactories = factories.length;
let lastPromise = head;
for (let i = 0; i < numFactories; i++) {
const factory = factories[i];
lastPromise = lastPromise.then(() => {
const promise = factory.apply(null, parameters);
assert.notEqual(promise, undefined, 'factory result is undefined');
assert.notEqual(promise, null, 'factory result is null');
assert.typeOf(promise, 'object', 'promise');
assert.typeOf(promise.then, 'function', 'promise.then');
return promise;
});
}
return lastPromise;
}
factories is a parameter that contains an array of function that return a promise each, when invoked. Other two parameters are optional. You can cut asserts out.