0
        var response = fetch('http://localhost:8082/tracks/read/'+trackid)
          .then(
            function(response) {
              if (response.status !== 200) {
                console.log('Looks like there was a problem. Status Code: ' +
                  response.status);
                return;
              }
              console.log('Fetch Success')
              response.json().then(function(dataData) {
                console.log(dataData.album.name);
                return dataData.album.name;
              });
            }
          )
          .catch(function(err) {
            console.log('Fetch Error :-S', err);
          });

New to javascript, having some real trouble working out promises and such.

All I want to do is be able to access the dataData.album.name outside of the function or method or variable or whatever this stuff is.

Whenever I set dataData.album.name to a variable it immediately becomes undefined once it is out of the scope of the promise

Simply can't work it out, would love any assistance

SimonQAC
  • 3
  • 1
  • Remember that promises are _asynchronous_! This means that they run in the background during (or after) the other code runs. You cannot return anything from an asynchronous call. The callback (what's inside `.then()`) is ran at some point in the future, whenever it's ready. You'll have to run all code that has anything to do with `dataData.album.name` inside this function. Or have a function that you can pass `dataData.album.name` to that can run to do what you need (once that value has been retrieved). – gen_Eric Oct 28 '20 at 17:30
  • @SimonQAC Use another `then` after you check the status. –  Oct 28 '20 at 17:35

0 Answers0