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?
Asked
Active
Viewed 1,177 times
1 Answers
0
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.

Lukáš Křivka
- 953
- 6
- 9
-
Thanks lucas. As I understand it from the documentation (https://sdk.apify.com/docs/api/apify#module_Apify.main) main() does exit the node.js process. When I run your code, this seems to be confirmed though it does log 'after main', but presumably that's because the main() function is async? – Rusty Oct 24 '19 at 15:27
-
I'm sorry, I was wrong. It indeed exits the Node process. The reason why the `after main` log works in that I passed async function inside. – Lukáš Křivka Oct 25 '19 at 19:41