Skip to content

Instantly share code, notes, and snippets.

@objectiveSee
Last active December 19, 2015 06:49
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 objectiveSee/5913832 to your computer and use it in GitHub Desktop.
Save objectiveSee/5913832 to your computer and use it in GitHub Desktop.
/**
* NOTE: game.participants contains 3 objects.
GOAL: I would like each promise created in lines 31-43 to be executed in serial.
I do not want any line of code in drawAnswers() to be called before the previous promise is fulfilled.
BUG: The log in drawAnswers() shows me that drawAnswers() is being called 3 times before the
1st promise from drawAnswers() is fulfilled.
SOME LOGS:
2013-07-02T22:12:56.897Z - info: Starting round. Game#
2013-07-02T22:12:56.898Z - info: [drawAnswers]:drawing answers for player [object Object]
2013-07-02T22:12:56.898Z - info: [drawAnswers]:drawing answers for player [object Object]
2013-07-02T22:12:56.898Z - info: [drawAnswers]:drawing answers for player [object Object]
2013-07-02T22:12:56.899Z - info: [drawAnswers]: Success
2013-07-02T22:12:56.899Z - error: [drawAnswers]: Not enough cards in Deck for game# 51d34d627eb7ce3b03000004 - building new deck.
2013-07-02T22:12:56.899Z - error: [drawAnswers]: Not enough cards in Deck for game# 51d34d627eb7ce3b03000004 - building new deck.
*/
Game.prototype.fillHands = function() {
var game = this;
// make promises
var promises = _.map(game.participants, function(participant) {
// draw cards
// drawAnswers() always returns a promise
return game.deck.drawAnswers(participant, HAND_SIZE - participant.hand.length)
.then(function(cards) {
participant.hand = _(participant.hand).union(cards);
return game;
});
});
// Execute the promises
return Q.all(promises);
/////// /////// /////// /////// /////// /////// /////// ///////
Deck.prototype.drawAnswers = function(player, count) {
var deck = this;
var game = deck.game;
var queue = this.queueName('answer');
Logger.debug('[drawAnswers]:drawing answers for player '+player._id);
if ( !game ) {
throw new Error('[drawAnswers]: Logic Error: No game for deck!');
}
// Create array of N promises (N=count). Each promise returns one card
var answerPromises = _(count)
.chain()
.range()
.map(function() {
return Q.ninvoke(redisClient, 'lpop', queue)
.then(function(cardId) {
if(cardId) {
return Q.ninvoke(Card, 'findById', cardId)
.then(function(answer) {
// verify that answer exists... cause we no like when things fall apart :(
if ( answer ) {
return answer.gameCard;
} else {
console.error('[drawAnswers]: no card to return. GameID=', deck.game.id);
return undefined;
}
});
}
});
})
.value();
// Execute all the promises.
return Q.all(answerPromises)
.then(function(answers) {
answers = _(answers).compact();
if( answers.length < count ) {
Logger.warning('[drawAnswers]: Not enough cards in Deck for game# '+game.id+' - building new deck.');
return this.build(game) // use module.exports to call external functions
.then(deck.drawAnswers(player, count));
} else {
Logger.debug('[drawAnswers]: Success');
return answers;
}
});
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment