0

New js user: I'm trying to use async with .then to make sure an ajax script has time to complete before I try to access its result. My ultimate goal here is a crude unit-test, I want to make sure an ajax function completed its task. I can't seem to get the code to wait properly. This example strips away as much of the complication as I can and attempts to leave the bare-bones problem:

async function tempfun2() {
  //in the "real" function, this is where time-consuming work communicating with server is done
  return 1;
}

function tempfun1() {

  tempfun2().then(

    function(value) {
      console.log("value in then = " + value);
      
      if (0 == 1) //in the "real" function, this is where I'd check to see if the work was done
      {
        return 0;
      } else {
        return 1;
      }
    )
  }


  let q = tempfun1();
  console.log("value_after_fun = " + q);

when I execute the code, the "value after fun" alert comes before the one within the .then statement. In a sense, I think this is working as designed, because I'm clearly escaping tempfun1() while tempfun2() is still working. However, this isn't what is desired, I need to make sure tempfun2() finishes so I can check that the work was complete AFTER it finishes.

I tried using several combinations of .then as well as using async/promises and had the same problem. I can't seem to wait for tempfun2() to finish before I escape tempfun1().

I've reviewed several similar help topics here, and they all seem to agree that promises or .then is the way to go, but I'm clearly missing something very basic here.

isherwood
  • 58,414
  • 16
  • 114
  • 157
  • `tempfun1` has no `return` statement. It calls `tempfun2`, sets up a `then` callback which will run when the promise resolves, then gets to the end and returns `undefined`. Later the promise will resolve and the anonymous function will return `0` or `1` which will be ignored because there isn't any code which pays attention to the promise returned by `then`. – Quentin Jan 24 '23 at 16:11
  • (You'll probably find it easier if you replace `then()` with `await`. https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Promises is probably worth a read). – Quentin Jan 24 '23 at 16:13
  • Yes, you are succinctly describing the problem, but I still don't understand the solution. I tried await() as well with the same result. I am able to get asynchronous functions to work "properly" using either .then or await - where I'm struggling is executing a practical followup using the result of the async function. This question was marked as a duplicate, and the link provides a very thorough explanation of the problem but I'm still missing how this actually answers the question. The example shows how to dump to console but that doesn't help me actually run a useful test on the value. – William Peltzer Jan 24 '23 at 16:33
  • If you want to return something from `tempfun1` then the first step is to use `return` to return something. – Quentin Jan 24 '23 at 16:34
  • The duplicate has 117 answers and thousands of words devoting to explaining how to do this in different ways. If you don't understand any of them then having another duplicate isn't likely to change that. The MDN page linked to above also explains in a different way. If none of that helps then perhaps you should look for a mentor instead. – Quentin Jan 24 '23 at 16:35
  • Hah, you volunteering?Fair enough, I'll continue to search there. I'm not averse to due diligence, but I do feel that after a certain number of hours spent rereading the same answers it's OK to ask for help with "stupid questions." – William Peltzer Jan 24 '23 at 16:39

0 Answers0