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

161
Views
Promise or Async

Hopefully someone can help me correct the below code. I am not able to get anything in the final return statement. I know it looks messy as I have tried a few alternatives.

async function concatnames(allSelectedRows){
var flightName = [];
var name = "";
var resultOutCome = "";
try {
    var result = await allSelectedRows.forEach(
        function (selectedRow, i){
            recordidGuid = selectedRow.getData().getEntity().getEntityReference().id.replace('{', '').replace('}', '') //Flight Plan Event Guid 
            name = Xrm.WebApi.online.retrieveRecord("crd80_flightplanevent", recordidGuid, "?$select=_crd80_learnerflight_value")                              
            }) 
            resultOutCome = name["_crd80_learnerflight_value@OData.Community.Display.V1.FormattedValue"]
            console.log(resultOutCome)
            flightName.push(resultOutCome);            
    }
    catch (error) {
            DisplayError(error)
    }
    return flightName
}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

await only works with promises, allSelectedRows.forEach(...) it's not a promise, it's a function.

You want to do something like this:

await Promise.all(allSelectedRows.forEach(
  async function (selectedRow, i) {
    recordidGuid = await selectedRow.getData().getEntity().getEntityReference().id.replace('{', '').replace('}', '') //Flight Plan Event Guid 
    name = await Xrm.WebApi.online.retrieveRecord("crd80_flightplanevent", recordidGuid, "?$select=_crd80_learnerflight_value")
  }))

The Promise.all() method takes an iterable of promises as an input, and returns a single Promise that resolves to an array of the results of the input promises. This returned promise will resolve when all of the input's promises have resolved, or if the input iterable contains no promises.

And the new allSelectedRows.forEach(...) with now async functions, returns promises, because when you do async function NameOfFunction, you are actually creating a promise, that Promise.all() will return all them resolved.

And I am using an await in Xrm.WebApi.online.retrieveRecord("crd80_flightplanevent", recordidGuid, "?$select=_crd80_learnerflight_value") because I think it is a promise, so when I add await, it will return me the resolved value of the promise.

about 4 years ago · Juan Pablo Isaza Report

0

Depending on what you to accomplish, there's a couple of ways to do this:

Process Each Request Sequentially

This is (a) slower than the alternative, and (b) more code. However, it gives you granular control of the error(s) encountered:

// process each request sequentially
async function concatnames( allSelectedRows ) {
  const flightName = [];
  let name = "";
  let resultOutCome = "";

  try {

    for ( const selectedRow of allSelectedRows ) {
      recordidGuid = selectedRow
                     .getData()
                     .getEntity()
                     .getEntityReference()
                     .id
                     .replace('{', '')
                     .replace('}', ''); 
      name = await Xrm
                   .WebApi
                   .online
                   .retrieveRecord(
                     "crd80_flightplanevent",
                     recordidGuid,
                     "?$select=_crd80_learnerflight_value"
                   );
      resultOutCome = name["_crd80_learnerflight_value@OData.Community.Display.V1.FormattedValue"];
      flightName.push(resultOutCome);
    }

  }
  catch (error) {
    DisplayError(error)
  }
  return flightName
}

Run the requests in parallel This is nice and clean. Also faster than the above because all request are run in parallel:

async function concatnames(allSelectedRows) {
  const flightName = await Promise.all( allSelectedRows.map( row => {
    recordidGuid = selectedRow
                   .getData()
                   .getEntity()
                   .getEntityReference()
                   .id
                   .replace('{', '')
                   .replace('}', '');
    return Xrm.WebApi.online
           .retrieveRecord(
             "crd80_flightplanevent",
             recordidGuid,
             "?$select=_crd80_learnerflight_value"
           )
           .then( name => name["_crd80_learnerflight_value@OData.Community.Display.V1.FormattedValue"]);
  }))
  .catch( DisplayError );

  return flightName || [];
}
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!