3

How do I apply the async/await feature to this code? I need to wait for the reltionshipQuery and forEach to finish before I return contentSpecies.

        contentSpecies = function(value, key, data) {
          var contentSpecies = [];

            objectID = data.OBJECTID;

            var queryTask = new QueryTask({
                url: "https://webmaps.com/MapServer/1"
            });

            var relationQuery = new RelationshipQuery({
                objectIds: [objectID],
                outFields: ["Species", "Notes", "Status"],
                returnGeometry: true,
                relationshipId: 0
            });

            queryTask.executeRelationshipQuery(relationQuery)
            .then(function(rslts) {
                var features = rslts[objectID].features;
                features.forEach(function(ftr) {
                    var t = ftr.attributes;
                    var species = t.Species;
                    contentSpecies += "<span class='bold' title='Species'><b>Species: </b></span>" + species + "<br/>";
                    var notes = t.Notes;
                    contentSpecies += "<span class='bold' title='Notese'><b>Notes: </b></span>" + notes + "<br/>";
                    var status = t.Status;
                    contentSpecies += "<span class='bold' title='Status'><b>Status: </b></span>" + status + "<br/>";

                });

                //return contentSpecies;
            })
            return contentSpecies
        }

2 Answers 2

1
contentSpecies = async function(value, key, data) {
  try {
    ...
    let rslts = await queryTask.executeRelationshipQuery(relationQuery);
    var features = rslts[objectID].features;
    features.forEach(function(ftr) {
    ...
  } catch(e) {
    console.error(e);
  }
}
1
  • it is not executing. can't even get a console log to work.
    – Jay Hill
    Commented Jun 25, 2018 at 19:18
0
async function xxx(){    

 var results = await queryTask.executeRelationshipQuery(relationQuery)


}

It works for me - see Using async await in ArcGIS JavaScript API

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.