javascript - how to exit from a node js script if all process completes -


i'm new node js. i'm using loop initiate same function multiple times different values. i'm using request scrape data website. if completed process script doesn't end automatically. due many reasons request connection not closed etc. it's running asynchronously can't able close manually. if use process.exit(); in last line of loop, gets closed before running function. how quit script if loop completes? please me.

my working code is:

const request = require('request'); const cheerio = require('cheerio'); const sequelize = require('sequelize'); var redis = require("redis"), client = redis.createclient(); var id = [20434, 21794, 21825, 21350, 20753, 20451];  const sequelize = new sequelize('xyz', 'root', 'root', {     host: 'localhost',     dialect: 'mysql',     pool: {         max: 5,         min: 0,         idle: 90000,         acquire: 90000     }, }); const user = sequelize.define('abcd', {     'x': {         type: sequelize.string     },     'y': {         type: sequelize.string     },     'a': {       type: sequelize.integer     },     'abc': {       type: sequelize.string     },     'info': {       type: sequelize.string     }, },{   tablename: 'xyz' });  for(var x=0; x<id.length; x++){     scrapeweb(id[x], x); }  function scrapeweb(id, text, done){     try {         switch(text) {             case 0:                 lang = "english";                 break;             case 1:                 lang = "hindi";                 break;             case 2:                 lang = "tamil";                 break;             case 3:                 lang = "malayalam";                 break;             case 4:                 lang = "kannada";                 break;             case 5:                 lang = "telugu";                 break;             default:                 lang = "language"                 break;         }         request("http://www*xyz*com/jfkdh/"+id+"/", function(error, response, body) {             if(error) {                 console.log(error);                 var err = new error('exception in requesting url')                 throw err             }                        //scrap given url , song-names                 ..... ..... ..... .....                 for(var i=0; i<text.length; i++){                      //db section check song available or not.                     user.findone({attributes: ['a', 'b'],                         where: {                             b: { $like: '%'+word[i]+'%'} ,                             x: "....."                         }                     }).then(data => {                         if(data != null) {                             //redis section                             client.on("error", function (err) {                                 console.log(err);                                 var err = new error('exception in redis client connection')                                 throw err                                                             });                             if(client.hexists('word', data.datavalues['a']) == true) {                                 client.hincrby('word', data.datavalues['a'] , 1);                             } else {                             // client.hincrby('word', 'count', 1);                                 client.hmset('word', data.datavalues['a'], 0);                                 client.expire('word', 86400);                             }                             console.log(data.datavalues['b']+" available , language = "+lang);                         } else {                         console.log("word not avilable");                         }                     });                 }             });         });     } catch(err) {         console.log(err);          } } 

edit function "scrapeweb" returns promise each call. while calling in loop, store promises in array may check "promise.all". on completion of "promise.all", script stop or may desire action.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -