Martin Hutchins
Martin Hutchins

Reputation: 39

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
}

Upvotes: 1

Views: 271

Answers (2)

Nicholas Carey
Nicholas Carey

Reputation: 74385

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

Upvotes: 2

Said Torres
Said Torres

Reputation: 653

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.

Upvotes: 3

Related Questions