返回诺言后,在JavaScript中使用等待
我有一个需要执行的函数。我使用GLTF解析器来检索顶点和面部。但是,它不会在正确的时间执行。这是我当前拥有的功能。
public parseGltfBlobs(toAdd: asmTreeNodeScene[]) {
const loader = new GLTFLoader();
for (let i = 0; i < toAdd.length; i++) {
let componentId: number = toAdd[i].componentId;
let isPart: boolean = toAdd[i].type == "part";
// Handle surface mesh requests for parts for server endpoint in json format
if (isPart) {
let surfaceMeshKey: string = componentId.toString() + "." + this.asmStateServ.decimation.toString();
// Grab the byte array
let gltfBytes = <ArrayBuffer>this.assemblyFetchServ.surfaceMeshesGltf.get(surfaceMeshKey)['gltfBytes'];
return new Promise((resolve, reject) => {
loader.parse(gltfBytes, "", gltf => {
let mesh;
let vertices;
let faces;
mesh = <THREE.Mesh>gltf.scene.children[0];
vertices = mesh.geometry.attributes.position.array;
faces = mesh.geometry.index.array;
let surfaceMesh: Object = {
"component_id": componentId,
"vertices": vertices,
"faces": faces,
}
resolve(this.assemblyFetchServ.surfaceMeshes.set(surfaceMeshKey, surfaceMesh));
console.log("Stored data in surfaceMeshes map")
// Emit finished event
}
)
})}
}
}
该功能正在以下方式调用。
this.requestMissingResources(toAdd, this.isGLTF).subscribe(
() => {
},
(err) => {
console.log(err);
},
async () => {
if(this.isGLTF) {
console.log("Parsing gltf blobs");
await this.parseGltfBlobs(toAdd);
console.log("End of parsing gltf blobs")
}
但是,我仍然没有收到输出承诺。我想知道为什么那是什么,我在这里做错了什么?如果给我正确的代码,这将很大程度上意味着我对我很陌生,这对我有很大帮助。
I have a function that needs to be executed. I used the GLTF parser to retrieve the vertices and faces. However it does not execute in the right time. This is the function that I currently have.
public parseGltfBlobs(toAdd: asmTreeNodeScene[]) {
const loader = new GLTFLoader();
for (let i = 0; i < toAdd.length; i++) {
let componentId: number = toAdd[i].componentId;
let isPart: boolean = toAdd[i].type == "part";
// Handle surface mesh requests for parts for server endpoint in json format
if (isPart) {
let surfaceMeshKey: string = componentId.toString() + "." + this.asmStateServ.decimation.toString();
// Grab the byte array
let gltfBytes = <ArrayBuffer>this.assemblyFetchServ.surfaceMeshesGltf.get(surfaceMeshKey)['gltfBytes'];
return new Promise((resolve, reject) => {
loader.parse(gltfBytes, "", gltf => {
let mesh;
let vertices;
let faces;
mesh = <THREE.Mesh>gltf.scene.children[0];
vertices = mesh.geometry.attributes.position.array;
faces = mesh.geometry.index.array;
let surfaceMesh: Object = {
"component_id": componentId,
"vertices": vertices,
"faces": faces,
}
resolve(this.assemblyFetchServ.surfaceMeshes.set(surfaceMeshKey, surfaceMesh));
console.log("Stored data in surfaceMeshes map")
// Emit finished event
}
)
})}
}
}
The function is being called in the following way.
this.requestMissingResources(toAdd, this.isGLTF).subscribe(
() => {
},
(err) => {
console.log(err);
},
async () => {
if(this.isGLTF) {
console.log("Parsing gltf blobs");
await this.parseGltfBlobs(toAdd);
console.log("End of parsing gltf blobs")
}
However, I still do not receive the output promise. I was wondering why that is and what I am doing wrong out here? It would mean a lot if the correct code is given to me as that would help me out a lot as I am very new to this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从评论中移动答案。
创建诺言定义回调以将其标记为成功或失败。您将解决方案使用并拒绝回调,因为
resolve(data)
或拒绝(数据)
将帮助您正确处理承诺。尝试/捕获可用于捕获这些输出。使用试用/捕获以上述等待的承诺处理承诺。
有什么异步,您需要承诺解析它吗?这是。如果是这样,您可能应该等待它以及另一个等待。如果不是,您甚至可能根本不需要承诺。
因此,我的意思是:
如果这是一个异步功能,则应等待它首先完成。如果不是,您可能甚至不应该首先使用承诺。
我建议通过 https:https:https://开发人员。 mozilla.org/en-us/docs/web/javascript/Reference/global_objects/promise 小心。回到过去,它有助于我理解自己的保证。
Moving answers from comments.
When creating a promise define callbacks to mark it as completed successfully or failed. You use resolve and reject callbacks for it as so:
The
resolve(data)
orreject(data)
will help you correctly handle the promise. Try/catch works on catching these outputs.use try/catch to handle promises with await as above.
Is there anything asynchronous that you require a promise to parse it? Is this.assemblyFetchServ.surfaceMeshes.set asynchronous? If so you probably should wait for it to complete as well with another await. And if not you probably don't even need a promise at all.
So what I mean is:
If that's an asynchronous function you should wait for it to complete first. And if it is not you probably shouldn't even use promises in the first place.
I'd recommend reading through https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise carefully. Back in time it helped me a lot to understand promises myself.