0
    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

TARIK
  • 19
  • 2
  • Does this answer your question? [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Heretic Monkey Mar 29 '22 at 11:29
  • what the heck is happening in this code wow – mstephen19 Mar 29 '22 at 11:37
  • It looks like you are using some kind of mapping or GIS tool here- when you ask a question it is useful to explain any libraries you are using and also what some of the functions do - for example that `featureLayer.createQuery()` looks like some kind of geodata query tool? It would be good to know which one. If that isn't relevant to your question then can you formulate a simpler version of the code that only includes the relevant parts? When I do this, I often solve the problem for myself just trying to write the question. – glenatron Mar 29 '22 at 13:47

1 Answers1

0

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.

glenatron
  • 11,018
  • 13
  • 64
  • 112