Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda