Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
How to execute non-blocking statements in JavaScript?

I'd like to execute a code (JavaScript, web) in the background that is non-blocking for the user. In other words, I want to call a function, but I don't want the following lines to wait for the call to finish, as it may last a while (and it not necessary for the rest of the program).

Maybe something like this:

function someBigFunctionIDontWantToWaitFor() {
  let i = 0;
  while (i < 100000000) {
    i++;
  }
  console.log("i =", i);
}

console.log("before call");
someBigFunctionIDontWantToWaitFor();
console.log("after call");

Obviously, in the console, I've this:

before call
100000000
after call

I'd like to have the following result:

before call
after call
100000000

Because someBigFunctionIDontWantToWaitFor takes time and the console.log("after call") doesn't want to wait.

Is this possible? Does it make sense?

Thank you

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can put the function in a then block of a Promise.

async function someBigFunctionIDontWantToWaitFor() {
        let i = 0;
        while (i < 100000) {
            i++;
        }
        console.log("i =", i);
  }
  
  console.log("before call");
  Promise.resolve(undefined).then(someBigFunctionIDontWantToWaitFor)
  console.log("after call");
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!