admin管理员组文章数量:1323530
This is what I want: I have a large array called results. Each item in that array I want to put in my iteration function and have it saved to my mongoDB (I omitted some steps here as well which are not important). After every single one of those items has been saved to the database (or refused) I want console.log to display 'All done'. Unfortunately All done is displayed almost immediately after calling the series while everything else is still processing. How do I do this right?
I am using mongoose with a model called 'light' and for readability I omitted all err messages from the code. I use node-async for the eachSeries part
var async = require('async');
var results = [very big array]
var iteration = function(row,callbackDone) {
light.find({data: row.data}, function (err,entry) {
if(entry.length) {
callbackDone();
return console.log(entry + ' already exists');
}
var newEntry = new light(row);
newEntry.save(function (err,doc) {
console.log(entry + ' saved');
callbackDone();
});
});
};
async.eachSeries(results,iteration, function (err) {
console.log('All done');
});
This is what I want: I have a large array called results. Each item in that array I want to put in my iteration function and have it saved to my mongoDB (I omitted some steps here as well which are not important). After every single one of those items has been saved to the database (or refused) I want console.log to display 'All done'. Unfortunately All done is displayed almost immediately after calling the series while everything else is still processing. How do I do this right?
I am using mongoose with a model called 'light' and for readability I omitted all err messages from the code. I use node-async for the eachSeries part
var async = require('async');
var results = [very big array]
var iteration = function(row,callbackDone) {
light.find({data: row.data}, function (err,entry) {
if(entry.length) {
callbackDone();
return console.log(entry + ' already exists');
}
var newEntry = new light(row);
newEntry.save(function (err,doc) {
console.log(entry + ' saved');
callbackDone();
});
});
};
async.eachSeries(results,iteration, function (err) {
console.log('All done');
});
Share
Improve this question
edited Feb 12, 2014 at 14:13
JohnnyHK
312k69 gold badges630 silver badges475 bronze badges
asked Feb 12, 2014 at 13:07
elmaltoelmalto
1,0481 gold badge14 silver badges23 bronze badges
2
-
1
Try to add some error loggers (
if (err) console.log(err);
). – maxdec Commented Feb 12, 2014 at 14:15 - I removed the error loggers for readability. sorry if I wasnt clear. The array easily has 100+ entries, the mongoose operations work flawlessly but the saves all happen after console.log('all done') has been displayed. What I need is a way to display 'all done' exactly after all items in my array have been processed – elmalto Commented Feb 12, 2014 at 14:57
1 Answer
Reset to default 4(An answer, not a ment, because the text was too long...)
I'm using async for something too and your question puzzled me. I tried the code below, getting the results even further down. It worked fine for me. (async 0.2.9). Even changing my timeout delays to 0.
The only thing that looked odd was the if(entry.length)
block printed console information after the callback. If you were getting a lot of "already exists" messages I could see potentially them ing later - but that doesn't sound like what you are describing.
var async = require('async');
var console = require('console');
var results = [ ];
results.push( {id:1,data:'a'} );
results.push( {id:2,data:'b'} );
results.push( {id:3,data:'c'} );
results.push( {id:4,data:'d'} );
function doFind( obj, callback ) {
setTimeout( function() {
console.log( "finding id=" + obj.data.id );
obj.data.length = obj.data.id % 2;
callback( null, obj.data );
}, 500 );
}
function light( obj ) {
this.id = obj.id;
this.data = obj.data;
this.save = function( callback ) {
setTimeout( function() {
console.log( "saving id=" + obj.id );
callback( null, this );
}, 500 );
};
}
var iteration = function(row,callbackDone) {
doFind({data: row}, function (err,entry) {
if(entry.length) {
callbackDone();
return console.log( 'id=' + entry.id + ' already exists');
}
var newEntry = new light(row);
newEntry.save(function (err,doc) {
console.log( 'id=' + entry.id + ' saved');
callbackDone();
});
});
};
async.eachSeries(results, iteration, function (err) {
console.log('All done');
});
Results
finding id=1
id=1 already exists
finding id=2
saving id=2
id=2 saved
finding id=3
id=3 already exists
finding id=4
saving id=4
id=4 saved
All done
本文标签: javascriptnodejs asynceachSeries calling final callback too earlyStack Overflow
版权声明:本文标题:javascript - node.js async.eachSeries calling final callback too early - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742122698a2421796.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论