0

I am writing a program using JavaScript that begins with generating a random array of strings. I wanted to use these strings in requests to an API (Scryfall), and then use the JSON sent back for other functions within the same program. So after running my initial few functions I want to end up with an array of objects pulled from the JSON located at the target API, to then be used throughout the rest of the script that is running.

The following is how I am requesting the data from the API:

fetch('https://api.scryfall.com/cards/named?exact=Lightning+Bolt')
        .then(response => response.json())
        .then(data => ***); //the *** is just a placeholder

let exteriorData = {};

From what I understand, I can only use the data I have requested within that chain of .then() following my fetch. Is there any way to copy the data object to the exteriorData object?

Thank you.

John
  • 1
  • 1
  • 3
    yes ... `exteriorData = data` in `***` ... but you won't know when it's there – Bravo Nov 26 '20 at 07:36
  • You call some callback inside `then` to execute any logic with `exteriorData` – Justinas Nov 26 '20 at 07:38
  • 2
    or ... `let exteriorDataPromise = fetch('https://api.scryfall.com/cards/named?exact=Lightning+Bolt').then(response => response.json())` ... and where ever you want to access `exterorData` just use `exteriorDataPromise.then(exteriorData => ***` ... or `exteriorData = await exteriorDatapromise;` in an `async` function – Bravo Nov 26 '20 at 07:38

0 Answers0