前言
上图中,点击播放icon,图中左边地球视角会按照视角列表依次执行。u3D提供了api,但是我们如何保证在循环中依次执行。即第一次执行完成后,再走第二次循环。很多人的第一思路就是promise。对,不错,出发的思路是正确的,但是要怎么组合呢?
// 设置摄像机镜头
export const setCamera = (config: any)=>{const { cimInstance } = window;return new Promise((resolve, reject) => {cimInstance.api.setCameraLook(config, (result:any)=>{resolve(result);})});});
}export const setCameraByArray = async (configArray:any[]=[])=>{for (const config of configArray) {await setCamera(config )}}
setCameraByArray(arrList)
总结
- 返回promise
export const setCamera = (config: any)=>{return new Promise((resolve, reject) => {function(执行代码块)});
}
- async await执行
export const setCameraByArray = async (configArray:any[]=[])=>{for (const config of configArray) {await setCamera(config )}}
3.调用
setCameraByArray(arrList)
踩坑 用for of 代替for in、forEach等