What is the equivalent of the arrow function?

1

This code is on a button. Two doubts,

How to write this line without using arrow function?

And whenever I run the first time it gets me undefined but in the second it works, I guess I have to add promises? How do you resolve this?

        let results;
        this.$refs.myMap.$mapObject.data.toGeoJson((geojson) => {
          results = JSON.stringify(geojson, null, 2);
        });
        console.log( results );
    
asked by anonymous 21.08.2017 / 01:23

2 answers

2

In this case it does not matter whether you have arrow function or an anonymous function, so you can simply change% from% to% with%. But if you do this for compatibility, you notice that .toGeoJson((geojson) => { should also be changed to .toGeoJson(function(geojson){ .

Regarding the asynchronous problem you can use promises, or use as is (with callbacks). Anyway what you have to do is put the code that needs this let inside of the function. That is, create a code stream from this callback.

Using "old" JavaScript (without var and without results ):

this.$refs.myMap.$mapObject.data.toGeoJson(function(geojson){
    var results = JSON.stringify(geojson, null, 2);

    // aqui podes chamar uma outra função que precise de 'results'
    console.log(results);
});
    
21.08.2017 / 05:59
0
let results;
this.$refs.myMap.$mapObject.data.toGeoJson(function(geojson){
     results = JSON.stringify(geojson, null, 2);
});
console.log( results );

Without using arrow functions

    
21.08.2017 / 02:04