0

I have a program that returns true or false correspondingly based on the result of an asynchronous function someAsyncFunction(), which looks like the follow:

function getResult(param) {
    someAsyncFunction(param).then((result) => {
        if (result == 1) {
            console.log('breakpoint 1');
            return true;
        } else {
            console.log('breakpoint 2');
            return false;
        }
    }
}

In the client file, I call the function as follow

const result = getResult(11);
console.log(result);

It seems that someAsyncFunction was able to function properly. The console prints 'breakpoint 1' or 'breakpoint 2' correspondingly based on the input param. However, the result printed is always undefined. Any ideas?

  • 1
    The function runs asynchronously. That means that `getResult()` doesn't wait for it before it returns. – Barmar Jul 31 '23 at 17:01
  • The return value of `getResult` won't be the actual result but a promise that resolves to the result. – AlgoPro Jul 31 '23 at 17:02

0 Answers0