Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

92
Views
array Length not reporting

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
       })

Console log

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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 do return flightName the array is still empty, and thus console.log(flightName.length) prints 0. The output from console.log(flightName) might pretend, that flightName contains values, but actually it does not because that console.log is referencing the object, which is later filled when the promises resolve. Do console.log(JSON.stringify(flightName)) and you will see it prints just [] because at that moment, the array is still empty

To resolve that issue, make the first then handler async and use await Xrm.WebApi... or properly wrap that whole thing in a Promise.all

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!