This should be simple, but it doesnt make sense to me. It is a simple section of a promise chain
let flightName = [];
let guidArr = [];
Promise.all(guidArr)
.then(values => {
for(var a = 0; a < values.length; a++) {
Xrm.WebApi
.online
.retrieveRecord("crd80_flightplanevent", values[a], "?$select=_crd80_learnerflight_value")
.then(
function success(result) {
flightName.push(result["_crd80_learnerflight_value@OData.Community.Display.V1.FormattedValue"])
},
function(error) {
DisplayError(error)
});
}
return flightName
}).then(flightName => {
console.log(flightName)
console.log(flightName.length)
return flightName
})
The console displays the flightName array correctly, but flightName.length is always consoled as 0, even though console.log(flightName) puts length:2 as the output
Why??? I need to work with each item in the array but it is not recognised correctly
The recommendation by @derpircher worked:
You are not awaiting the promises in your
for(var a = 0; a < values.length; a++)loop. Thus, when you doreturn flightNamethe array is still empty, and thusconsole.log(flightName.length)prints0. The output fromconsole.log(flightName)might pretend, thatflightNamecontains values, but actually it does not because thatconsole.logis referencing the object, which is later filled when the promises resolve. Doconsole.log(JSON.stringify(flightName))and you will see it prints just[]because at that moment, the array is still emptyTo resolve that issue, make the first
thenhandlerasyncand useawait Xrm.WebApi...or properly wrap that whole thing in aPromise.all