Assign AJAX call result to variable

If you want to manipulate the data immediately after the request has finished, you have to do the body of work inside one of the callbacks passed to then, due to the asynchronous nature of fetch() promises.

You can assign the response to a variable like you’re doing in the example above, but you won’t know when it’ll be available. If you’re handling the response inside the same action as you’re fetching it, just enclose it in a promise:

load() {
  fetch("https://your-api-url")
    .then(response => response.json())
    .then(json => {
      if (/* Some requirement */) {
        // Do what needs to be done
      }
    })
}