0

I have read endless answers about promises in javascript and accessing the data from them using the .then but I still cant seem to figure this out...

I have the following function

function fetchOHLC(yUrl) {
    return fetch(yUrl)

        .then(response => response.json())
        .then(function(response) {
            var t = response.items[0].contentDetails.duration;

            return {
                t
            };
        })
        .catch(function(error) {
            console.log(error);
        });
}

and I am trying to access the value of t by doing the following

var fetchData = fetchOHLC(yUrl);
var value= fetchData.then(function(value){
  console.log(value.t);
  return(value.t);
});

The console.log logs the correct value I am looking for, but I cannot seem to actually save this value to a variable.

The variable value is still equal to the Promise Object.

Any ideas on how I can save the values I am getting in the console.logs to a variable that can be used outside of the promise itself?

wazz
  • 4,953
  • 5
  • 20
  • 34

0 Answers0