'Sending parameters from different functions to a function

    function layerQuery(objectId) {
    const featureLayer = view.map.layers.getItemAt(0);
    const queryParams = featureLayer.createQuery();
   

    queryParams.where = "objectid=" + objectId;
    queryParams.outFields = ["x", "y", "z"];

    featureLayer.queryFeatures(queryParams).then(function (results) {
      const coords = results.features[0].attributes;
      direction_lookup(Promise, Promise,coords.x,coords.y)
    });
    const objectIdNext = objectId + 1
    const queryParamsTb = featureLayer.createQuery();
    queryParamsTb.where = "objectid=" + objectIdNext;
    queryParamsTb.outFields = ["x", "y", "z"];
    featureLayer.queryFeatures(queryParamsTb).then(function (results) {
      var coordstb = results.features[0].attributes;
      direction_lookup(coordstb.x, coordstb.y,Promise,Promise)
    });
    //console.log(coordstb.x)
    
  }

I want to send the four parameters we obtained as a result of the above two functions to the following function.

function direction_lookup(destination_x, origin_x, destination_y, origin_y) {
    var compass_brackets, compass_lookup, degrees_final, degrees_temp, deltaX, deltaY;
    deltaX = destination_x - origin_x;
    deltaY = destination_y - origin_y;
    degrees_temp = Math.atan2(deltaX, deltaY) / Math.PI * 180;
    

    if (degrees_temp < 0) {
      degrees_final = 360 + degrees_temp;
    } else {
      degrees_final = degrees_temp;
    }

    compass_brackets = ["N", "NE", "E", "SE", "S", "SW", "W", "NW", "N"];
    compass_lookup = Math.round(degrees_final / 45);
    return [compass_brackets[compass_lookup], degrees_final];
  }

  console.log(direction_lookup(destination_x, origin_x, destination_y, origin_y));

The 'direction lookup' function takes four parameters. I want to send these four parameters two by two from different functions. can i do this



Solution 1:[1]

What you're trying to do isn't possible because your direction_lookup function needs those four parameters to be the numeric co-ordinates, it isn't expecting to see any Promise objects or resolve them. The standard way to do this is to wait until you know all the values before you call the function, like this:

function layerQuery(objectId) {
    const featureLayer = view.map.layers.getItemAt(0);
    const queryParams = featureLayer.createQuery();
   

    queryParams.where = "objectid=" + objectId;
    queryParams.outFields = ["x", "y", "z"];

    featureLayer.queryFeatures(queryParams).then((results) => {
      const feature1Coords = results.features[0].attributes;
      const objectIdNext = objectId + 1
      const queryParamsTb = featureLayer.createQuery();
      queryParamsTb.where = "objectid=" + objectIdNext;
      queryParamsTb.outFields = ["x", "y", "z"];
      featureLayer.queryFeatures(queryParamsTb).then((secondResults) => {
         const feature2Coords = secondResults.features[0].attributes;
         direction_lookup(feature2Coords.x, features2Coords.y,feature1Coords.x,feature1Coords.y);
      });
    }); 
   }

Now you perform your first query and keep the results from that in scope while you perform the second query and finally call direction_lookup when you have both sets of co-ordinates ready to go.

Arrow functions behave like regular functions but maintain the parent scope, which can save a lot of confusion, so for this kind of task it's a slightly more convenient notation.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 glenatron