javascript - Ordering flow in asynchronous logic with long-ish callbacks -


the basic flow of program receive udp packets, info on them, send them off further processing. info stored in mongodb.

pseudo-js like:

socket.on('message', function(message) {     mongo.lookup(message, function(err, object) {         furtherprocessing(object);     }); }); 

i'm noticing odd/annoying behavior in flow.

since mongodb asynchronous, observe program flow switches next packet while waiting lookup respond. means that, if many packets arrive, might have multiple mongodb requests interleaved (i.e. program awaiting callback multiple packets). unfortunately, responses mongo seem in random order (or perhaps order satisfied mongo), means, time objects passed furtherprocessing function, may reordered.

is there way enforce fifo ordering on async requests? is, way me guarantee every object sent futherprocessing in same order received socket?

i think you'd have code logic sort of queue array.

you save messages in order come in, , whenever of async tasks complete, flag 1 complete , send @ front queue complete further processing.

this way if message 2, 3, 4 complete, won't sent until message 1 completes. 4 fly in same runloop.

// outer queue save messages var queue = [];  socket.on('message', function(message) {    // packet comes in, add queue   queue.push(message);    // start async lookup   mongo.lookup(message, function(err, object) {      // mark message compelte     message.complete = true;      // loop through queue     (var = 0; < queue.length; i++) {        // message task completed?       if (queue[i].complete) {          // further process item         furtherprocessing(queue[i]);         // found first incomplete message!       } else {          // remove processed items far queue         // making new array index onward         queue = queue.slice(i);          // stop loop         return;       }     }   } }) 

working example: http://jsfiddle.net/khp3v/

the async lookup in example randomly take between 0ms , 500ms, ensures out of order-ness. if @ console, list them in order processed.


Comments