Can you use a return in a fetch response?

39 Views Asked by At

So, I am fetching a JSON from a URL and have it in a response then. In the section where my response/data is read, I would like to use a return. I need to do so, because I am making a module for "Magic Mirror" and in order to do so, you have to create a element and then return it so that it gets displayed to the main page. Well, as already said, I fetch the data, like to read it, assign the fetched JSON data to a element and then that one has to be returned. But I am unsure if it is even possible to use a return inside a fetch response. If not, how would I go about this? Maybe something like a global variable or are there simpler/other things?

This is my code

Module.register("test",{
// Default module config.
defaults: {
},

getData: function() {

    function foo() {
     return fetch("http://transport.opendata.ch/v1/connectionsfrom=Watt, 
     Dorf&to=Regensdorf Watt,Bahnhof&limit=2&transportations=bus&direct=1")
     .then(function(response) {
         return response.json();
        })
    };

    foo().then(function(response){
        msg = data.connections[0].from.station.name;
        var wrapper = document.createElement("h1");
        wrapper.innerHTML = msg;
        return wrapper;
    });
    }
});

Thanks, in advance.

0

There are 0 best solutions below