Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get returned value from an 'async' function - Javascript

Tags:

javascript

I am working with some asynchronous functions in javascript, but I am facing a problem, that I already posted here but that was a bit unpractical experience for everyone. Now, I made a simple constructor function with same member functions inside and returned a value, but seems like same problem to me, I tried my best but I don't know what is the problem, if you run this code then You can check what I want. Here's the demo link on JSfiddle, where you can see the results on console.

This is my Code

function Test() {
  this.init = async function() {
    var count = 0,
      page_job_details = null;

    async function waitMore() {
      console.log("Wait more loaded - " + (count + 1) + "...");
      let p = new Promise(function(res, rej) {
        setTimeout(async function() {
          if (count === 2) {
            res({
              page_job_details: "khan"
            });
          } else {
            count++;
            waitMore();
          }
        }, 2000);
      });
      var res = await p;
      if (res.page_job_details === '' || res.page_job_details === 'undefined' || res.page_job_details === null) {
        console.log("waiting more...");
        waitMore();
      } else {
        console.log("Response is : " + res.page_job_details);
        return res;
      }
    }
    var khan;
    await waitMore().then(function(r) {
      console.log(r);
      khan = r;
    });
    return khan;
  }
}
new Test().init().then(function(res) {
  console.log(res);
})

When you comment out the conditions within the setTimeout() and simply res({page_job_details:"khan"}); then you'll get the results in the new Test().init().then(function(res){ console.log(res); }). Otherwise not, and that's the main problem.

like image 858
Nadeem Ahmad Avatar asked Jul 03 '26 04:07

Nadeem Ahmad


1 Answers

One of the issues is that you are not returning the result of the recursive call from within the promise.

Instead of just calling it recursively

waitMore();

you seem to expect the result of the recursive call to be returned down the pipeline

res(waitMore());

function Test() {
  this.init = async function() {
    var count = 0,
      page_job_details = null;

    async function waitMore() {
      console.log("Wait more loaded - " + (count + 1) + "...");
      let p = new Promise(function(res, rej) {
        setTimeout(async function() {
          if (count === 2) {
            res({
              page_job_details: "khan"
            });
          } else {
            count++;
            res(waitMore());
          }
        }, 2000);
      });
      var res = await p;
      if (res.page_job_details === '' || res.page_job_details === 'undefined' || res.page_job_details === null) {
        console.log("waiting more...");
        waitMore();
      } else {
        console.log("Response is : " + res.page_job_details);
        return res;
      }
    }
    var khan;
    await waitMore().then(function(r) {
      console.log(r);
      khan = r;
    });
    return khan;
  }
}
new Test().init().then(function(res) {
  console.log(res);
})
like image 141
Wiktor Zychla Avatar answered Jul 04 '26 17:07

Wiktor Zychla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!