Getting back value of a variable within a function

195
3
Jump to solution
a month ago
KaiBehncke
New Contributor III

Dear users,

 

I use in ArcGIS Maps SDK 4.29 "projection" to reproject coordinates:

    require([
          "esri/geometry/projection"
], function(projection) { ....

 

I do it in that way:

projection.load().then(function() {
var punkt2=projection.project(punkt1, cs2);
alert ("In Function: "+punkt2.x);
});

alert ("Ausserhalb Function: "+punkt2.x);

Within the function it works fine. "alert ("In Function: "+punkt2.x);"

shows the correct coordinate value I want to have.

But outside the function: How can I get the value then? I `m looking for a way to get the value back to use it outside the function.

Could anybody help?

 

0 Kudos
1 Solution

Accepted Solutions
JeffreyThompson2
MVP Regular Contributor

You should move your additional logic to within the .then() function or to functions that are called from within the .then() function.

GIS Developer
City of Arlington, Texas

View solution in original post

3 Replies
JeffreyThompson2
MVP Regular Contributor

In your case, I think the easiest thing to do would be to define the variable outside the function and then set the value inside the function. Like this.

var punkt2 = null

projection.load().then(function() {
punkt2=projection.project(punkt1, cs2);
alert ("In Function: "+punkt2.x);
});

//As writen, punkt2.x will likely be undefined, but would eventually be correct.
alert ("Ausserhalb Function: "+punkt2.x);

Be aware that Javascript executes synchronously, so it will attempt to run every line of code without waiting for earlier lines to complete. The .then() function causes the code inside the parenthesis to wait for the projection.load() function to complete before executing. As the code block above is written, punkt2.x will likely be undefined because the alert() function will likely execute faster than the load() function will complete. Given more time to execute, the punkt2.x would eventually become correct. 

GIS Developer
City of Arlington, Texas
0 Kudos
KaiBehncke
New Contributor III

Thank you very much for your reply, but in that case, it`s not the solution.

In my case I use an adress-search with an own REST-Service. After searching for an adress I have a code like...

getResults: (params) => {...

....

I get results there and later on in the code

I successfully get them by:

return searchResult;

But: In the "gettingResults-area" I have to reproject some coordinates.

As far as I can see the only possibility in using the "projection.project"-function is within an:

projection.load().then(function() {


punkt2=projection.project(punkt1, cs2);
alert ("1:"+punkt2.x);
});

 

....so far so good. But I have to do some other things with the result outside of that function, like:

alert ("2:"+punkt2.x);

var x_koordinate=punkt2.x;
var y_koordinate=punkt2.y;

const graphic = new Graphic({
geometry: new Point({
x: x_koordinate,
y: y_koordinate
})
,attributes: feature.attributes
});

const buffer = geometryEngine.geodesicBuffer(
graphic.geometry,
100,
"meters"
);

 

....but I don`t get the value out of the projection.load().then(function() { }. That is the problem.

0 Kudos
JeffreyThompson2
MVP Regular Contributor

You should move your additional logic to within the .then() function or to functions that are called from within the .then() function.

GIS Developer
City of Arlington, Texas