score:6

Accepted answer
let promiseOfEntries = Promise.all(Object.entries(githubAPI).map((entry) => {
  let [key, value] = entry;
  return fetch().then(x => x.json()).then(result => {
    // Important step here: We combine the result with the key,
    // into a new [key, value] entry
    return [key, result];
  })
}

let promiseOfResults = promiseOfEntries.then(resultEntries => {
  // From the "entries", we build an object again here
  let results = {};
  for (let [key, value] of resultEntries) {
    results[key] = value;
  }
  return results;
});

promiseOfResults.then(results => {
  // Do anything with results!
  console.log('results:', results);
});

score:1

const movieData = {};
// Added keyTracker array declaration here...
const promiseArr = [], keyTracker = [];
for (const user in githubAPI) {
  movieData[user] = fetch().then(res => res.json())
}
console.log(movieData);
for (const user in movieData) {
  // Track the order of the promises by saving the key in our keyTracker
  keyTracker.push(user);
  promiseArr.push(movieData[user]);
}

Promise.all(promiseArr).then(responseArr => console.log(responseArr));

// Now we have a lookup for the keys (keyTracker) that will allow us to 
// set the movieData correctly.
keyTracker.forEach((key, idx) => {
  movieData[key] = promiseArr[idx];
});

score:2

const movieData = responseArr.reduce((mD, response, index) => ({ ...mD, [orderOfKeys[index]]: response }), {})

Related Query

More Query from same tag