Return 来自 getBulkProperties Autodesk Forge 的值

Return value from getBulkProperties Autodesk Forge

我想 return 来自 viewer.model.getBulkProperties2 成功回调的数据像这样

  getOmniValues(){
       return this.viewer.model.getBulkProperties2([],{propFilter:["OmniClass_21_Numero"]},(val)=>val)
  }

但我认为函数 return 无效。我怎样才能做到这一点?

getBulkProperties2 method and many other viewer methods are asynchronous,这意味着它们不能 return 立即得到结果,因为它们通常需要等待另一个异步操作,例如 Web 请求或长 运行 任务另一个线程。这就是为什么您通常需要提供一个回调函数,该函数稍后会在 Web 请求或 long-运行 任务完成时调用。

如果你想避免 callback hell, you can wrap the method in a Promise 然后使用更现代的 async/await JavaScript:

function getProps(model, dbids, options) {
    return new Promise(function (resolve, reject) {
        model.getBulkProperties2(dbids, options, resolve, reject);
    });
}

// Note the `async` keyword below which is required if we want to `await` something inside the function
viewer.addEventListener(Autodesk.Viewing.GEOMETRY_LOADED_EVENT, async function (ev) {
    const props = await getProps(viewer.model, [123, 456]);
    console.log(props);
});