0

Firstly The code below prints the value from the call() function which it returns though it takes some time to execute, then it prints the next line.

But I want to make it async behavior so that it first outputs the last line(which execution time is less) than the previous (higher exec time). How can I do that?

function call() {
  let counter = 0;
  for (let i = 0; i < 40000; i++) {
    for (let j = 0; j < 50000; j++) {
      counter++;
    }
  }
  return counter;
}
console.log(call());
console.log('loading ... ');
mplungjan
  • 169,008
  • 28
  • 173
  • 236
  • No matter the order, neither will log until `call` has finished running. – evolutionxbox Feb 16 '21 at 12:49
  • Why do you want to do that? If it's just to get a different output order it would be much easier to just swap the lines. What else do you hope to achieve with that? – Felix Kling Feb 16 '21 at 12:54
  • duplicate of [this](https://stackoverflow.com/questions/54478255/why-is-my-infinite-loop-blocking-when-it-is-in-an-async-function) and [this](https://stackoverflow.com/questions/11488014/asynchronous-process-inside-a-javascript-for-loop) – uingtea Feb 16 '21 at 13:16
  • 2
    Does this answer your question? [Asynchronous Process inside a javascript for loop](https://stackoverflow.com/questions/11488014/asynchronous-process-inside-a-javascript-for-loop) – uingtea Feb 16 '21 at 13:16

2 Answers2

-1

By adding the key word async before the fonction so it becomes

async function call(){...}

i hope it will helps

-1

Asynchronous functions

Asynchronous functions are built on top of promises. They allow a more convenient use of Promises. Asynchronous functions have the following properties:

  • async before a function declaration/expression turns the function into an async function. As the name suggests async function are executed asynchronously.
  • An async function always returns a promise. It wraps any returned value in Promise.resolve(returnval). However, when an uncaught error is thrown inside the async function it wraps the return value in Promise.catch(returnval).
  • Inside async functions you can use the await keyword which can be used before any promise. await makes JS code execution stop until the promise is settled. i.e. The promise has to be either fulfilled or rejected until any further code inside the async function is executed.
  • await either returns the value of the fulfilled promise, or throws an error in the case of a rejected promise. We can use regular try-catch to catch the error.

Let's clarify this with some examples:

Example1:

const randomProm = new Promise((resolve, reject) => {
    if (Math.random() > 0.5) {
        resolve("Succes");
    } else {
        reject("Failure");
    }
});

// async keyword creates an async function which returns a promise 
async function ansyncExample() {

    try {
        const outcome = await randomProm;
        console.log(outcome);
    } catch (error) {
        console.log(error);
    }

    // This return value is wrapped in a promise
    return 'AsyncReturnVal';
}

// ansyncExample() returns a promise, we can call its corresponding then method
ansyncExample().then((value) => {
    console.log(value);
});

console.log('I get executed before the async code because this is synchronous');
  • *"As the name suggests async function are executed asynchronously."* That's not true. `async` implies two things: The function returns a promise and you can use `await` inside the function to suspend execution. `async` alone doesn't mean that the function is executed asynchronously. Just run `(async function() { console.log('inside'); return 42; }()); console.log('after');`. Or to put in another words: Everything until the first `await` expression is executed synchronously. – Felix Kling Feb 16 '21 at 12:57