使用 async await來(lái)操作函數(shù)異步,函數(shù)體內(nèi)有遞歸,但是遞歸還沒(méi)有完成就執(zhí)行完了函數(shù)。then里面的返回值是個(gè)數(shù)組但是數(shù)組打印的長(zhǎng)度是1,但真實(shí)長(zhǎng)度應(yīng)該是5,打斷點(diǎn)調(diào)試res的第一個(gè)返回值是一個(gè)長(zhǎng)度為1的數(shù)組,但是緊接著就是長(zhǎng)度為5的數(shù)組,await不是應(yīng)該等getAllDownRoteEquipment函數(shù)執(zhí)行完了再返回值嗎?請(qǐng)求大神解答 getAllDownRoute(){ viewer.vmethods.getAllDownRoteEquipmentArr().then(function(res){ console.log(res) }) } async getAllDownRoteEquipmentArr(id){ var equipmentArr = []; var selectIds = viewer.getSelection()[0]; await viewer.vmethods.getAllDownRoteEquipment(selectIds,equipmentArr); return equipmentArr; } getAllDownRoteEquipment(id,equipmentArr){ return new Promise(function (resolve) { var aa = (id,equipmentArr)=>{ viewer.getProperties(id,function(res) { for (var i = 0; i < res.properties.length; i++) { if (res.properties[i].displayName == "型號(hào)") { var downEquipment = res.properties[i].displayValue; equipmentArr.push(downEquipment); resolve(equipmentArr); viewer.search(downEquipment,(searchId) =>{ aa(searchId[0],equipmentArr) },function(){},"自身標(biāo)識(shí)"); break; } } }) } aa(id,equipmentArr) }) }
js await的函數(shù),這個(gè)函數(shù)里又有遞歸,還沒(méi)有執(zhí)行完就執(zhí)行了
回首憶惘然
2019-03-15 14:15:49