I try to implement a state machine using node js. to simplify the callbacks I'm using q and promises. In some cases calling functions on a specific state does nothing. but to use it in control flow, I need a resolved promise.
So my state looks like this.
// onstate.js
module.exports = function(){
this.switchOn = function(){
var deferred = Q.defer();
deferred.resolve();
return deferred.promise;
};
this.switchOff = function(){
var deferred = Q.defer();
object.switchOff()
.then(function(result){
// changing to off-state
deferred.resolve();
},
function(err){
deferred.reject(err);
});
return deferred.promise;
};
}
The switch off function is like described in Q docu. but what about the switchon function? I wand to call:
currentState.switchOn().then(foo(), bar());
Do I really have to create a deferred resolving immediately or can the same behavior be achieved using a more simple code?