Is there a way to use Apify.main() without it exiting the node.js process on completion?

Viewed 1048

I'm using the Apify SDK in my app, and have written a number of scrapers using the Apify.main() function. The final action of main() is to exit the node process, but this does not suit my purposes. Is there any way to over-ride this behavior?

1 Answers

You don't need to use Apify.main or you can simply continue with code after it. Apify.main doesn't exit the process. The following code runs fully:

const Apify = require('apify');

Apify.main(async () => {
    console.log('main');
});
console.log('after main');

The main reasons for Apify.main are:

1) Ensure that the function inside it completes early so it doesn't wait on callbacks like your top-level code.

2) It emits some events.

Related