0

I had a coding challenge where this just stumped me:

function runMultipleQueries(queries) {
  var results = [];
  queries.forEach(doQuery);
  return results;
  function doQuery(query) {
    getData(query)
      .then(results.push.bind(results));
  }
}

function log(value) {
  console.log(value);
}

runMultipleQueries(someArrayOfQueries).forEach(log);

Edit: Assume these functions are all defined elsewhere in script.

What would be the hypothetical output of this codeblock? I'm getting stumped on when the promise gets returned, and what is printed in the meantime? I was thinking I would see an array of UNDEFINED, but now im not sure!

HiYo
  • 3
  • 2

1 Answers1

2

what is printed in the meantime? I was thinking I would see an array of UNDEFINED

No. The promises were not yet fulfilled, so no push was executed yet, and the array is still empty. That's why nothing is printed - and will never be.

To get something printed, you will need with printing until the promises are fulfilled - either separately (printing each result when it becomes available) or all together (putting the results in an array and then printing that). For the latter, you would not push them yourself though, you'd rather use Promise.all:

function runMultipleQueries(queries) {
  return Promise.all(queries.map(getData));
}

function logArray(results) {
  for (var value of results)
    console.log(value));
}

runMultipleQueries(someArrayOfQueries).then(logArray);
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Ok help me understand, I thought a promise acts as a placeholder and moves on to the next line of code until its resolved (gets a returned object) – HiYo Aug 30 '17 at 00:19
  • I thought the point of a promise is so JavaScript doesn't block the thread and wait for something to be returned – HiYo Aug 30 '17 at 00:21
  • @HiYo No, a promise is not a placeholder that will get replaced with the result. It's a wrapper. There are no threads in JS. It's asynchronous, which means it always immediately moves on to the next line of code - and when the promise gets fulfilled later, it will call the `then` callbacks. [There is no magic](https://stackoverflow.com/a/22562045/1048572) that makes normal code "wait". – Bergi Aug 30 '17 at 00:23
  • Ok, so if the promise is getting fulfilled later, are we skipping the .then() and moving to the next iteration? I'm sorry I need to understand this step by step :( – HiYo Aug 30 '17 at 00:40
  • Yes, exactly, the asynchronous process is started, installs the handler using `then`, and immediately moves on to the next iteration. – Bergi Aug 30 '17 at 00:43
  • ok thanks, so when would JS go back to fulfill the promises, if at all? Also which part is exactly the promise, the .then function? (sorry for the qty of questions) – HiYo Aug 30 '17 at 02:08
  • JS doesn't go back. The asynchronous process it started will at some time, and resolve the promise. The details of that are in `getData`, where the promise is created. Yes, `then` is a promise method. – Bergi Aug 30 '17 at 02:59