0

I want to get status from async function but it always return status undefined and than it goes to function that contains return status;

main.js

 let status = addData(firstname, lastname, age, sex);
 console.log(status);

data.js

static async addData(firstname, lastname, age, sex) {
  try {

    let txdata = await data.dataentry(firstname, lastname, age, sex);

    let count = 0;
    let intervalId = setInterval(async() => {
      let status = await data.entryStatus(txdata);
      logger.info("status %o", status);


      if (status = true) {
        clearInterval(intervalId);
        return status;
      } else {
        count++;
        if (count > 4) {
          clearInterval(intervalId);
          throw createError();
        }
      }
    }, 500);

  } catch (err) {
    throw createError();
  }
}
Hassan Abbas
  • 1,166
  • 20
  • 47
  • 2
    You have an assignment, not condition check in **if** block – Damask Sep 19 '17 at 11:38
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Jared Smith Sep 19 '17 at 11:55

1 Answers1

1

Standard way to return result from async function is to use Promise. It is quite similar to described by Attila but you don't need a library to use it.

It works in all modern browsers and nodejs / iojs so if you don't care about Internet Explorer, it is the best option as I think. If you need Internet Explorer, you may use shim. "shim promise" in Google gives several options but I didn't try any.