0

I'm using Promise.all() for the first time, hopefully, this isn't too ignorant...

I'm unable to access my array with dot notation or bracket notation. I have two objects in the array, each with a key of 0 and then 1.

I get undefined when I try using indices and bracket notation.

Here is an image of it all...

I'm using Promise.all() for the first time, hopefully, this isn't too ignorant...

    // GET ALL POKEMON NAMES
    let fetchAll = fetch(`https://pokeapi.co/api/v2/pokemon/? 
   limit=20&offset=0`);
    let fetchEach = fetch("https://pokeapi.co/api/v2/pokemon/1");

    let dataArr = []

    Promise.all([fetchAll, fetchEach])
        .then(files => files.forEach(file => process(file.json())))
        .then(console.log(dataArr[1].abilities))
        .catch(err => console.log(err))

    let process = (prom) => {
        prom.then(data => dataArr.push(data))
    }

I expect to be able to grab the object in the array but only get undefined.

Jacob Broughton
  • 366
  • 1
  • 5
  • 14
  • 2
    Your `process` isn't returning anything, so it's disconnected from the outer Promise chain. That's quite convoluted anyway, consider having a function that `fetch`es a URL and returns its `.json()`. Then: `Promise.all([getResp('? limit=20&offset=0'), getResp('1')]).then((dataArr) => /* use dataArr here */ })` – CertainPerformance Oct 04 '19 at 23:41
  • Thanks for the feedback, my way didn't seem like the most optimal way to go about it. Whats the '1' in the getResp referring to? Is that first object key a string? – Jacob Broughton Oct 04 '19 at 23:59
  • That's the last part of the URL you're fetching (a bit like your `fetchEach` variable, but less repetitive) – CertainPerformance Oct 05 '19 at 00:02

0 Answers0