I'm currently struggling to resolve a promise in my script. The promise needs to be resolved when a function containing 3D file imports finishes importing, but I'm unsure how to make this happen.
Is there a way to receive a signal or data from the browser indicating that the model has finished loading so the promises can execute?
Below is the code snippet of the promise that should execute res() once generateContent() completes importing objects:
const myGeneralAsyncPromise = new Promise((res, rej) => {
generateContent();
if(some condition) res();
// else rej();
});
myGeneralAsyncPromise.then(allIsReady, notReadyYet);
The following section invokes a class that creates various objects within generateContent():
var arrow3 = body(scene, world, 'static', 'arrow', { hx: 0.2, hy: 0.2, hz: 0.2 }, { x: 34.5, y: 3.35, z: 6 }, { x: 0, y: 11, z: 0 });
bodys.push(arrow3);
var phone = body(scene, world, 'static', 'phone', { hx: 1.3, hy: 1.3, hz: 1.3 }, { x: 35.35, y: 1.8, z: 6.5 }, { x: 0, y: 0, z: 0 });
bodys.push(phone);
var pencil = body(scene, world, 'static', 'pencil', { hx: 2, hy: 2, hz: 2 }, { x: 35.5, y: 1.8, z: 14 }, { x: 0, y: 11, z: 0 });
bodys.push(pencil);
The subsequent code block handles the actual import of each object:
function body(scene, world, bodyType, colliderType, dimension, translation, rotation) {
new GLTFLoader_js_1.GLTFLoader().load(`src/models/${colliderType}.glb`, function (gltf) {
var model = gltf.scene;
collider = gltf.scene;
model.scale.x = dimension.hx;
model.scale.y = dimension.hy;
model.scale.z = dimension.hz;
// Additional processing for model here...
scene.add(model);
});
}
It's worth noting that generateContent() involves other time-consuming processes besides imports, with the import being the most time-consuming task.
To summarize, I need to implement a condition in my main promise that will trigger res() upon completion of the model loading process.