About this, I'm having trouble implementing it..
so, to go with real code, it's like this
I've got
function melted(host, port, timeout) {
//// stuff...
this.commandQueue = Q.resolve();
}
melted.prototype.sendCommand = function(command) {
logger.debug(this.uuid + " - Sending command: " + command);
return this.mlt.sendCommand(command, "200 OK");
};
and then I've got a lot of methods that look like this
melted.prototype.stop = function() {
//Stop
return this.sendCommand("STOP U0");
};
that
this.mlt.sendCommand returns a promise that I want to return to the caller. Now, I've also got this special method
melted.prototype.sendClip = function(clip, command) {
/// stuff...
var deferred = Q.defer();
var self = this;
fs.writeFile(xmlFile, xml.toString({pretty:true}), function(err){
if (err) {
deferred.reject(err);
} else {
logger.debug(self.uuid + " - File ready: " + xmlFile);
deferred.resolve(self.sendCommand(command.replace("{xmlFile}", xmlFile)));
}
});
return deferred.promise;
};
Which is buggy, because the calls to sendCommand are not necessarily sent in the order that sendClip was called.
How can I make sure the calls to sendCommand are done in the same order as the calls to sendClip?
I've thought this out:
melted.prototype.next = function(then) {
var ret = Q.defer();
this.commandQueue = this.commandQueue.then(function() {
ret.resolve(function() {
return then();
});
});
return ret;
};
But I'm sure there must be some kind of shortcut, and I'm not even sure of how to use it, exactly, to avoid losing parallelization. i.e., I'd
like the calls to writeFile still to be made in parallel if possible, and just have the resolution of their callbacks to be done in the right order
Any clues?
Thank you
Tomas