蓝鸟协程根本不执行

bluebird coroutine no executing at all

我正在尝试实现剪刀石头布游戏的 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() 函数 returns 使用 new 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);
        });
    });
};

Promise.coroutine() "returns a function that can use yield to yield promises" (doc),但你实际上并没有调用那个函数。

您可能想要这样的东西:

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

Promise.coroutine 是一个高阶函数,即它需要一个生成器函数和 return 另一个函数,当调用该函数时,将 return 您正在寻找的承诺.正如@robertklep 所说,您甚至没有调用那个 returned 函数。

相反,您应该将完整的方法包装在 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');
    }
});