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

127
Views
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 answers
Answer question

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 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!