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

128
Visualizações
NodeJS Async main function not exiting

Working on a small NodeJS project currently which involves the Puppeteer library. The issue I'm having is that the main() function in index.js seems to never exit. All of the code runs fine, but in VS code it looks like the program is still running.

Some sample code below:

var UserAgent = require('user-agents');
const userAgent = new UserAgent();
const puppeteer = require('puppeteer-extra');

const StealthPlugin = require('puppeteer-extra-plugin-stealth');
puppeteer.use(StealthPlugin());

class TS {
    constructor() {
        let page;
    }
    
    async init() {
        const browser = await puppeteer.launch({ headless: false });
        this.page = await browser.newPage();
        await this.page.setUserAgent(userAgent.toString());        
    }
}


async function main(params) {
    console.log("Started main..");
    const ts = new TS();
    await ts.init();
    console.log("Ending main..");
}

main().catch(e => console.error(e));;

If I run the above code in a VS code terminal the output will show:

Started main.. Ending main.. (blank line)

The program will now stay in this state indefinitely until I hit ctrl + C. If I remove the awaited ts.init() call then it works as expected and my terminal shows

Started main.. Ending main.. PS C:\Users\username\Desktop\nodejs-projects\my-project>

Can anyone explain what's going on here?

From all of the research I've done the best I can come up with is some problem with the event loop, though really lost as to what's causing it.

Adding a .then() to main() with something like this solves the problem

    .then(() => {
    setTimeout(() => {
        process.exit(0);
    }, 5000);;
})

This proves that the promise is resolving since the setTimeout() is being called. However if main resolves and there is no code left to run I'm quite lost as to why the program won't automatically end without calling process.exit(). Would really appreciate some help.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

So funny. Been looking for a solution all day and as soon as I post it to stackoverflow I find it..

Found this package which can help point to things like open connections that prevent node from exiting: why-is-node-running

Next I found that the open puppeteer connection to the open chrome browser needs to be closed with something like this:

async close() {
    await this.browser.close();
}

Rather than delete the question I thought I'd post this in case anyone else has the same headache sometime.

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