返回诺言后,在JavaScript中使用等待

发布于 2025-02-09 18:19:21 字数 2279 浏览 1 评论 0原文

我有一个需要执行的函数。我使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2025-02-16 18:19:21

从评论中移动答案。

  1. 看来您的诺言没有/调用解决回调,甚至知道它已经完成。

创建诺言定义回调以将其标记为成功或失败。您将解决方案使用并拒绝回调,因为

new Promise((resolve, reject) => { ... do something ... resolve(data); })
  1. 我看不到任何收到承诺结果的东西,也没有从承诺中返回任何结果。如果您不打算从Promise中返回任何信息至少与布尔值联系,以将其标记为已完成,以便您知道您是否可以进一步进行。

resolve(data)拒绝(数据)将帮助您正确处理承诺。尝试/捕获可用于捕获这些输出。

try {
  // if promise resolved successfully you will get data in the data variable
  const data = await function_returning_a_promise();
}
catch (error) {
  // if promise rejected you will catch an error here
  console.error(error);
}
  1. 使用试用/捕获以上述等待的承诺处理承诺。

  2. 有什么异步,您需要承诺解析它吗?这是。如果是这样,您可能应该等待它以及另一个等待。如果不是,您甚至可能根本不需要承诺。

因此,我的意思是:

const result = await this.assemblyFetchServ.surfaceMeshes.set(surfaceMeshKey, surfaceMesh);

如果这是一个异步功能,则应等待它首先完成。如果不是,您可能甚至不应该首先使用承诺。

我建议通过 https:https:https://开发人员。 mozilla.org/en-us/docs/web/javascript/Reference/global_objects/promise 小心。回到过去,它有助于我理解自己的保证。

Moving answers from comments.

  1. Looks like your promise doesn't have/call the resolve callback to even know it is finished.

When creating a promise define callbacks to mark it as completed successfully or failed. You use resolve and reject callbacks for it as so:

new Promise((resolve, reject) => { ... do something ... resolve(data); })
  1. I don't see anything receiving the result of the promise anyway, nor returning any results from the promise. If you're not planning on returning any info from promise at least call resolve with a boolean in it to mark it as completed for you to know if you can proceed further or not.

The resolve(data) or reject(data) will help you correctly handle the promise. Try/catch works on catching these outputs.

try {
  // if promise resolved successfully you will get data in the data variable
  const data = await function_returning_a_promise();
}
catch (error) {
  // if promise rejected you will catch an error here
  console.error(error);
}
  1. use try/catch to handle promises with await as above.

  2. 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:

const result = await this.assemblyFetchServ.surfaceMeshes.set(surfaceMeshKey, surfaceMesh);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文