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

111
Views
Promise doesn't execute asynchronously

I have been testing promises on node.js in the following program:

const areallylongprocesspromise = new Promise((resolve, reject) => {
    let buff = 0;
    for (let i = 0; i < 1000000000; i++)
    {
        if ((i % 73829) === 0) buff++;
    }
    if (buff > 10) resolve(buff);
    else reject(buff);
});




areallylongprocesspromise.then((resolved) => 
{
    console.log("Resolved: ", resolved);
})
.catch((rejected) => {
    console.log("Rejected: ", rejected);
});

console.log("Waiting for execution to finish up");

The expected output is:

Waiting for execution to finish up
Resolved:  13545

However, the first statement, "waiting... up" doesn't get logged until the promise finishes execution, at which point both the statements get logged simultaneously. I'm new to the concept of promises, so I don't know what is going on here. Why is the promise holding up the execution of the rest of the code? Any help would be appreciated.

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

0

The code inside the Promise is not executed when you call .then, it is executed when you define the Promise itself - try just to log something at the beginning of the function:

const areallylongprocesspromise = new Promise((resolve, reject) => {
    console.log('start');
    ...
});

Since your code is synchronous, it holds the execution of the code by definition.

about 4 years ago · Juan Pablo Isaza Report

0

That happens because NodeJS and Javascript, despite allowing I/O async calls, they form single-threaded applications

It means that when the CPU is computing

    for (let i = 0; i < 1000000000; i++)
    {
        if ((i % 73829) === 0) buff++;
    }

It does not do anything else.

If you use for example setTimeout it works as expected because it creates an async timer that works as an I/O operation

const areallylongprocesspromise = new Promise((resolve, reject) => {
    let buff = 0;
    for (let i = 0; i < 1000000000; i++)
    {
        if ((i % 73829) === 0) buff++;
    }
    
    setTimeout(()=> {
       if (buff > 10) resolve(buff);
       else reject(buff);
    }, 1000)
});

areallylongprocesspromise.then((resolved) => 
{
    console.log("Resolved: ", resolved);
})
.catch((rejected) => {
    console.log("Rejected: ", rejected);
});

console.log("Waiting for execution to finish up");

This timer is useless here, just to show you how I/O async calls do work. That timer could represent for example reading a file, connecting to a DB or fetching an image from the internet, they are all I/O operations.

That's the magic of NodeJS, despite being single-threaded it does not block the code for I/O operations.

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!