Variable not incrementing as expected

See the following for more information about Promises:

Very difficult to read the code as it is not very well formatted, so I copied into an IDE to get a better look. Some questions, observations, and comments:

  • You have a data.items.forEach() loop that checks how many results?

  • In this loop, you are calling asynchronous functions with Promises that don’t get resolved before calling these functions again (as far as I can tell).

  • What is getJSON() ? Are you sure it’s executing and actually returning something?

  • Are you sure that the ( length > initialNearbyHotspots ) condition is resolved as TRUE? Try putting a console.log() right after it to see if the execution of the code even gets there and to see what’s happening.

  • If you get a lot of items returned from your query, you might end up with a very heavy loop.

There’s more, but to be honest, your code isn’t exactly simple, and it’s not too clear what it is you’re trying to accomplish.

Some of the Promise issues might be solved by using async/await instead of .then() .

Let’s see what we can do.