Skip to content

Instantly share code, notes, and snippets.

@GlenTiki
Last active August 29, 2015 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GlenTiki/55b42835fd65062be312 to your computer and use it in GitHub Desktop.
Save GlenTiki/55b42835fd65062be312 to your computer and use it in GitHub Desktop.
var obj = new ObjectThatDoesAsyncStuff(); //create object
// create some `when` checks - when these conditions are met the code in them should run
// (right after the variable has been changed, this async `if` should be run on it)
when(obj.someNumber > 10 && obj.someNumber < 20) {
console.log('its in range! Fire the lazer!!');
}
when(obj.commandReady) {
command = obj.takeACommand();
}
obj.doSomethingToSomeNumber(function(err){
if(err) return console.log('error occured')
if(obj.someNumber > 10 && obj.someNumber < 20){
console.log('gotta do my check here! Fire the lazer!!');
}
})
obj.addCommandToQueue('command to add', function(err){
if(err) return console.log('error occured')
if(obj.commandReady) {
command = obj.takeACommand();
}
})
obj.addCommandToQueue = function(command, cb){
obj.commandQueue.push(command);
obj.commandReady = true;
cb(null)
}
obj.takeACommand = function(command, cb){
// you might have to do something like this because of weird
// observation rules on the commandReady object
obj.commandReady = false
if(obj.commandQueue.length > 0) obj.commandReady = true;
return obj.commandQueue.pop()
}
obj.doSomethingToSomeNumber = function(cb){
obj.someNumber += 11;
cb(null)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment