node.js - Strange behaviour coding with events -
so working on application quite bit of database work, , thought 1 smart way of getting around should use events. set events emitter application use, , using way control flow. , kind of assumed events emitter work similar message queue or similar.
this not case, , reason can not emitter work expected on second call same function.
i have understood if problem called multiple times, is, below code run second function twice, passed dbevents.once('ready'
once. , not can not figure out why, seems work constantly, , never print out "wrote fourth"
var mongo = require('mongodb'); var events = require('events'); var dbevents = new events.eventemitter(); function writestuff(first, second, status, cuid){ console.log('wrote '+ first); if (status === 1) { console.log('\t\tready '+ cuid); dbevents.emit('ready'); } else { console.log('\t\tready (but not 1) ' + cuid); dbevents.emit('ready'); } console.log('\t\tjust kick of number ' + status); dbevents.once('ready', function (condata) { console.log('wrote '+ second) dbevents.emit('done'+cuid) }); } var cuid1 = mongo.objectid(); writestuff('first', 'second', 1, cuid1); dbevents.on('done'+cuid1, function(){ console.log('\tfirst completed ' + cuid1); }); var cuid2 = mongo.objectid(); writestuff('third', 'forth', 2, cuid2); dbevents.on('done'+cuid2, function(){ console.log("\tsecond completed " + cuid2) }); var b = 0; for(var = 0; < 100000000; i++){ b++; } console.log("and counted " + b);
the output when run above code looks this
wrote first ready 5540c57cd8fa9555076f9aba kick of number 1 wrote third ready (but not 1) 5540c57cd8fa9555076f9abb wrote second first completed 5540c57cd8fa9555076f9aba kick of number 2 , counted 100000000
================ update ==============
why can't generate event, i.e. if use unique identifier not seem can send event.in case change event ready+cuid (mongodb), , second write never gets called.
var mongo = require('mongodb'); var events = require('events'); var dbevents = new events.eventemitter(); function writestuff(first, second, status, cuid){ console.log('wrote '+ first); if (status === 1) { console.log('\t\tready '+ cuid); var flag = 'ready'+cuid dbevents.emit(flag); } else { console.log('\t\tready (but not 1) ' + cuid); dbevents.emit('ready'); } console.log('\t\tjust kick of number ' + status); dbevents.once(flag, function (condata) { console.log('wrote '+ second) dbevents.emit('done'+cuid) }); } var cuid1 = mongo.objectid(); writestuff('first', 'second', 1, cuid1); dbevents.on('done'+cuid1, function(){ console.log('\tfirst completed ' + cuid1); }); var cuid2 = mongo.objectid(); writestuff('third', 'forth', 2, cuid2); dbevents.on('done'+cuid2, function(){ console.log("\tsecond completed " + cuid2) }); var b = 0; for(var = 0; < 1000000000; i++){ b++; } console.log("and counted " + b);
it seems you're adding 'ready' listener after dispatching 'ready' event on first run. when fire of second run, first listener called. explains why 'wrote second' logged after 'wrote third' called.
move addition of listener beginning of writestuff().
function writestuff(first, second, status, cuid){ console.log('\t\tjust kick of number ' + status); dbevents.once('ready', function (condata) { console.log('wrote '+ second) dbevents.emit('done'+cuid) }); console.log('wrote '+ first); if (status === 1) { console.log('\t\tready '+ cuid); dbevents.emit('ready'); } else { console.log('\t\tready (but not 1) ' + cuid); dbevents.emit('ready'); } }
Comments
Post a Comment