Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

205
Views
Why doesn't this recursive solution work with asynchronous functions in Javascript, but works fine iteratively?

Been coding in JavaScript for about a year but I have this weird error/bug I have found a work around solution, but am curious on an explanation as to why.

So I have an asynchronous function receiving weather data from an API getWeatherStats() It returns a promise which is opened with the .then notation in another function called getCoordinates(). I simplified the weatherParser() function to show that if I make the solution recursive, it returns undefined, but if done iteratively it works fine.

getWeatherStats(city).then((data) => {
  console.log(data)
  var i = 0;
  var done = weatherParser(data, i);
  console.log(done); //returns undefined if done recursively, is fine if done iteratively
})


async function getWeatherStats(city) {
  var url = `http://api.openweathermap.org/data/2.5/forecast?q=${city}&appid=${weatherKey}`;


  let response = await fetch(url);
  let data = await response.json();
  var i = 0;
  return new Promise(resolve => {
    resolve(data);
  }, 2000);

Here's the recursive solution that returns null

function weatherParser(data, i, widgetHolder, date = null) {
  if (i > 20) return 'hello'
  i++;
  weatherParser(data, i, widgetHolder, date)

Iterative solution:

function weatherParser(data, i, widgetHolder, date = null) {
  for (var i = 0; i < 20; i++) {

  }
  return 'hello'
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!