-3

I want to exit node process when all data will be processed (fetch data, http request with axios and update data). When I run my script by typing node script.js it never ends. Where should I use process.exit() in my script to be sure all data has been processed? Script below:

Keyword.find({
        images: {
            $eq: []
        }
    })
    .limit(8)
    .exec((err, keywords) => {
        keywords.forEach((keyword) => {
            console.log(keyword.body);
            axios.get('https://www.example.com/?q=' + encodeURIComponent(keyword.body))
                .then(function (response) {
                    const dom = new JSDOM(response.data);
                    let items = dom.window.document.querySelectorAll('div.item');
                    let images = [];
                    items.forEach(((item) => {
                        let url = item.querySelector('a.thumb').href;
                        let source = item.querySelector('a.tit').href;
                        let description = item.querySelector('div.des').innerHTML;
                        images.push({
                            url,
                            source,
                            description
                        });
                    }));

                    Keyword.findByIdAndUpdate({
                        _id: keyword._id
                    }, {
                        $set: {
                            images
                        }
                    }, (err2, result2) => {
                        if (err2) console.log(err2);
                        console.log(result2);
                    });
                })
                .catch(function (error) {
                    console.log(error);
                });
        });
    });
Shant Marouti
  • 1,135
  • 3
  • 11
Joey Nie
  • 137
  • 1
  • 11
  • Possible duplicate of [How to exit in Node.js](https://stackoverflow.com/questions/5266152/how-to-exit-in-node-js) – Claus Jørgensen Aug 22 '18 at 18:23
  • If you're not looking for `process.exit(..)` Nothing in your code indicates you're blocking the runloop, which is why your code don't exit when it's done (assuming you run in a terminal environment, and not Electron et.al.). You should post mode code and rephrase your question – Claus Jørgensen Aug 22 '18 at 18:42

2 Answers2

3

Seems like your database connection is keeping your app from automatically exit. So you should either close the database connection or call process.exit() when you have done your work.

You are doing multiple async operations simultaneously

keywords.forEach((keyword) => {
    axios.get(...

To be notified when all the operations completed you can create a Promise array and listens to them by Promise.all() method

const operations = keywords.map((keyword) => axios.get('').then((response) => {
    ...
    return new Promise((resolve, reject) => {
        Keyword.findByIdAndUpdate(query, update, (err2, result2) => {
            if (err2) {
                console.log(err2);
                reject(err2);
            }
            console.log(result2);
            resolve();
        });
    });
}));

Promise.all(operations).then(()=>{
    console.log('All done.');
    process.exit();
});
Shant Marouti
  • 1,135
  • 3
  • 11
1

From the docs:

The process.exit() method instructs Node.js to terminate the process synchronously with an exit status of code. If code is omitted, exit uses either the 'success' code 0 or the value of process.exitCode if it has been set. Node.js will not terminate until all the 'exit' event listeners are called.

To exit with a 'failure' code:

process.exit(1);
Chaim Ochs
  • 119
  • 9