Empty results with Fetch function.s

I’m trying to get a working example of the https://adobexdplatform.com/plugin-docs/tutorials/how-to-make-network-requests/ guide. But my JSON results turn op empty.

I’ve been trying this for a while. Seems like I’m missing something?

console.log(getData("https://dog.ceo/api/breeds/image/random/"));

function getData(url) {
        return fetch(url)                                          
            .then(function (response) {
                return response;       
            })
}

This prints out:
{}

Well if anyone cares.

The problem was that I was logging the results before waiting for those ‘results’ to actually load.
PS found this gem in the UI faces plugin.

This does work:

async function getContentFromAPI(url){
        let response = await fetch(url);
        let json = await response.json();
        console.log("json:");
        console.log(json);
        return json;
}
getContentFromAPI('https://uifaces.co/api?r&limit=10');
2 Likes

async and await work like a charm. Thanks for posting the answer to your own question!

1 Like