Like the question, how to pass the formal parameter data in the closure to external variables
var outer;
var map = new BMap.Map("allmap");
var point = new BMap.Point(116.331398,39.897445);
var gc = new BMap.Geocoder();
gc.getLocation(point, function(rs){
var addComp = rs.addressComponents;
//how to store this string to variable outer;
//outer = addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
});
Using assignment can only ensure that the data in the closure is valid. It will be invalid after exiting. Using new to allocate memory has no effect. I am short of time and have no time to learn JS, so I am shameless to ask segmentfault>3<
Use ajax async:false but the problem remains?
Post the code and take a look. According to common sense, it should be OK.Other methods
1: Asynchronous values and logic related to asynchronous values can be processed in the then logic of promise.
2: Use generator and yield synchronization writing to process your logic
3: You can also use async function directly.
Your code assignment method is correct. The so-called [invalid after exiting] may be a control flow problem like this:
In your code,
gc.getLocation
If it is an asynchronous call like the above example, then the code execution order cannot be guaranteed according to the order in which the code is written. Therefore, if you access theouter
variable directly in subsequent code, you are likely to get the old value before the asynchronous call is completed.You can now define an external variable outside, such as the Object type, and then pass it in as a parameter. By assigning a value to the Object internally, you can transfer the value to the outside