【发布时间】:2021-10-08 13:21:22
【问题描述】:
我想像这样从viewer.model.getBulkProperties2成功回调中返回数据
getOmniValues(){
return this.viewer.model.getBulkProperties2([],{propFilter:["OmniClass_21_Numero"]},(val)=>val)
}
但我认为该函数返回 void。我怎样才能做到这一点?
【问题讨论】:
我想像这样从viewer.model.getBulkProperties2成功回调中返回数据
getOmniValues(){
return this.viewer.model.getBulkProperties2([],{propFilter:["OmniClass_21_Numero"]},(val)=>val)
}
但我认为该函数返回 void。我怎样才能做到这一点?
【问题讨论】:
getBulkProperties2 方法和许多其他查看器方法是asynchronous,这意味着它们不能立即返回结果,因为它们通常需要等待另一个异步操作,例如 Web 请求或另一个线程上的长时间运行的任务.这就是为什么您通常需要提供一个回调函数,以便稍后在 Web 请求或长时间运行的任务完成时调用该函数。
如果您想避免使用callback hell,可以将方法包装在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);
});
【讨论】: