What is the equivalent of Arrow Function?

Asked

Viewed 57 times

1

This code is on a button. There are two doubts,

How to write this line without using Arrow Function?

And whenever I run the first time it comes back Undefined me but the second one works, I think you have to add Promises? How do you solve this?

        let results;
        this.$refs.myMap.$mapObject.data.toGeoJson((geojson) => {
          results = JSON.stringify(geojson, null, 2);
        });
        console.log( results );

2 answers

2

In this case it doesn’t make any difference to have Arrow Function or an anonymous function, so you can just change .toGeoJson((geojson) => { for .toGeoJson(function(geojson){. But if you do it for the sake of compatibility let should also be changed to var.

In relation to 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 results within function. That is, create a code stream from that callback.

Using "old" Javascript (without let and without arrow function):

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);
});

0

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

No use Arrow functions

Browser other questions tagged

You are not signed in. Login or sign up in order to post.