How can i queue 2 async calls?


#1

Hi.
The most I’m investigating meteor and javascript in general, the most i’m figuring this kind of problems. Fiber / promise / wrapasync are still confused enough for me to find simple solution.
Here is my problem : I create a telnet server on my server side using npm net package. I put an callback method on de “on” data received to notify client that new data as arrived. To do that, I’m using a collection where I define key and Meteor.userId. All is working fine exept one thing : when I received the first time 2 super quick data. My collection test twice if there is a record with key + userId, and both return there is not… Then it insert 2 new records for the same couple key + userId instead of create once and update it the second times !

How can I queue (sync) this ?!

Here is some code :

// Server side code
this.createServer = function () {
    var connect = require('net');
    _telnetClient = connect.connect('8090', '192.168.1.234');
    _telnetClient.on('data', function (data) {
        var encodedBytes = data;
        UserSession.set("myKey", _myUserId, encodedBytes);
    });
}

// my UserSessionPackage
UserSession = {		// This set value from server with Meteor.userId() to the respective client
    set: function (key, userId, encodedBytes) {
        Fiber = Npm.require('fibers');
        Fiber(function() {
            var existing = UserSessionCollection.findOne({ key: key, userId: userId, tabId: tabId});

            var sv = {
                key: key,
                userId: userId,
                encodedBytes: encodedBytes
            };
            if (existing){
                UserSessionCollection.update({ _id: existing._id }, { $set: sv });
            } else {
                UserSessionCollection.insert(sv);
            }
        }).run();
    }
}

Thanks for your hints !