javascript - How to exit properly from node.js -


the following read , import many csv files disk mongodb nodejs won't exit after importing files if doesn't go through resizephoto() function (which contains process.exit after resizing images).

how can have close after importing files without interrupting? if add process.exit .on end exit after importing first file.

var importdata = function(filename) {      // file disk.     var filepath = path.join(folder, filename);      // read , import csv file.     csv.frompath(filepath, {         objectmode: true,         headers: keys     })     .on('data', function (data) {          var obj = new models[filename](data);          models[filename].find({}).remove().exec();          obj.save(function (err, importedobj) {             if (err) {                 console.log(filename, err);             } else if (filename === 'photos') {                 resizephoto(importedobj);             }         });      })     .on('end', function() {         console.log(filename + ': imported.');     }); };  module.exports = importdata; 

use module async, method parallel (https://github.com/caolan/async#parallel). can call tasks (import) in parallel , call final handler (exit) after tasks end.

in case:
1) somewhere in project

csvimp=require('importdata.js'); async.parallel([     function(done){csvimp(name1,done);},     function(done){csvimp(name2,done);},     ...     function(done){csvimp(namen,done);} ], function(err, results){     process.exit(); }); 

2) in importdata.js

var importdata = function(filename,done) { ... .on('end', function() {     console.log(filename + ': imported.');     done(); }); 

so, next need prepare list of tasks. like

names.foreach(function(n){     tasks.push(function(done){csvimp(n,done);}); }); 

and call async.parallel tasks.

good luck)


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

how to prompt save As Box in Excel Interlop c# MVC 4 -

xslt 1.0 - How to access or retrieve mets content of an item from another item? -