bluebird coroutine根本没有执行

时间:2015-11-14 19:34:30

标签: javascript promise command-line-interface bluebird

我正在尝试实现Rock Paper Scissors游戏的CLI版本。我正在使用查询器模块来处理IO。我的主要功能如下:

RockPaperScissors.prototype.gameLoop = function()
{
var x;
var Promise = require('bluebird');


//simple promise test
//this.playGame().then(function(){ console.log("The end");});

Promise.coroutine(function*()
{

    //for(x=0;x<this.maxTurns;x++)
    //{
        console.log('Printing '+ x.toString());
        var action = yield this.playGame();
    //}    

    if(this.playerScore > this.serverScore) { console.log('Player wins match');} else {console.log('Server wins match');  }    

});
};

exports.RockPaperScissors = RockPaperScissors;

playGame()函数返回使用新Promise()的声明。如果我这样做:

this.playGame().then(function(){ console.log("The end");});

承诺正确执行。但是,在Promise.coroutine()中使用时,不会执行任何操作。我在这里缺少什么?

这是playGame()函数的代码:

RockPaperScissors.prototype.playGame = function()
{

    var inq = require('inquirer');  
    var rand = require('random-js');
    var _ = require('lodash');
    var promise = require('bluebird');

    //make possibilities local
    var possibilities = this.possibilities;

    console.log ('------------------ Stats ----------------');
    console.log ('Player: ' +this.playerScore+'  Server: '+this.serverScore);
    console.log ('-----------------------------------------');

    var question1 ={
        type:'rawlist',
        name:'option',
        message:'Please choose Rock, paper or scissors:',
        choices:['Rock','Paper','Scissors']
    };

    return new promise(function(resolve,reject)
    {
       inq.prompt([question1],function(answers)
       {
            console.log('You chose '+answers.option);
            var playerObject = answers.option;
            //random with Mersenne Twister API
            var r = new rand(rand.engines.mt19937().autoSeed());
            var myPlay =r.integer(0,2);
            var serverObject ='';
            switch(myPlay)
            {
                case 0:
                    serverObject='Rock';
                    break;
                case 1:
                    serverObject ='Paper';
                    break;
                case 2:
                    serverObject='Scissors';
                    break;
            }

            var result='', action='';
            //choose winner by using a lodash function!
            _.forEach(possibilities,function(e){
                if (e[0]==serverObject && e[1] ==playerObject) 
                {
                     result=e[2];
                     action=e[3];
                }
            });

            console.log('I chose ' + serverObject+ "\n")
            console.log (result);

            if (action=='win') {this.playerScore++;}
            if (action=='lose'){this.serverScore++;}
            resolve(action);
        });
    });
};

2 个答案:

答案 0 :(得分:3)

Promise.coroutine是一个高阶函数,即它接受一个生成器函数并返回另一个函数,该函数在被调用时将返回您正在寻找的承诺。正如@robertklep所说,你甚至没有打电话给那个返回的功能。

相反,您应该将完整方法包装在Promise.coroutine中,而不是在方法中调用它。您的代码应如下所示:

var Promise = require('bluebird');

RockPaperScissors.prototype.gameLoop = Promise.coroutine(function*() {
    // simple promise test:
    // yield this.playGame();
    // console.log("The end");

    for (var x=0;x<this.maxTurns;x++) {
        console.log('Printing '+ x.toString());
        var action = yield this.playGame();
    }

    if (this.playerScore > this.serverScore) {
        console.log('Player wins match');
    } else {
        console.log('Server wins match');
    }
});

答案 1 :(得分:1)

Promise.coroutine() &#34;返回一个可以使用yield来产生承诺的函数&#34; doc),但你实际上并没有调用功能

你可能想要这样的东西:

Promise.coroutine(function*() {
...
})();
//^^ calling it