Javascript 蓝鸟协同程序根本不执行

Javascript 蓝鸟协同程序根本不执行,javascript,promise,command-line-interface,bluebird,Javascript,Promise,Command Line Interface,Bluebird,我正在尝试实现一个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(fu

我正在尝试实现一个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;
Promise.coroutine(function*() {
...
})();
//^^ calling it
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');
    }
});
承诺正确执行。但是,当在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()
“返回一个可以使用yield来产生承诺的函数”(),但实际上您并没有调用该函数

你可能想要这样的东西:

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;
Promise.coroutine(function*() {
...
})();
//^^ calling it
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');
    }
});
Promise.coroutine()
“返回一个可以使用yield来产生承诺的函数”(),但实际上您并没有调用该函数

你可能想要这样的东西:

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;
Promise.coroutine(function*() {
...
})();
//^^ calling it
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');
    }
});

Promise.coroutine
是一个高阶函数,即它接受一个生成器函数并返回另一个函数,调用该函数时,将返回您正在寻找的Promise。正如@robertklep所说,您甚至没有调用返回的函数

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

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;
Promise.coroutine(function*() {
...
})();
//^^ calling it
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');
    }
});
var Promise=require('bluebird');
RockPaperScissors.prototype.gameLoop=Promise.coroutine(函数*(){
//简单承诺测试:
//放弃这个;
//控制台日志(“结束”);
对于(var x=0;x this.serverScore){
console.log(“玩家赢得比赛”);
}否则{
log('Server wins match');
}
});

Promise.coroutine
是一个高阶函数,即它接受一个生成器函数并返回另一个函数,调用该函数时,将返回您正在寻找的Promise。正如@robertklep所说,您甚至没有调用返回的函数

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

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;
Promise.coroutine(function*() {
...
})();
//^^ calling it
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');
    }
});
var Promise=require('bluebird');
RockPaperScissors.prototype.gameLoop=Promise.coroutine(函数*(){
//简单承诺测试:
//放弃这个;
//控制台日志(“结束”);
对于(var x=0;x this.serverScore){
console.log(“玩家赢得比赛”);
}否则{
log('Server wins match');
}
});

为了让它正常工作,我不得不做一些额外的修改。例如,我必须保存对“this”对象的引用,并将其作为参数传递给生成器函数,因为在内部承诺时,this上下文似乎丢失了(绑定到不同的值)。我还必须将其转换为playGame()函数。我仍然不知道为什么承诺会这样做。@tutiplain
bluebird
有一种方法,您可以使用它来设置函数的
this
。比如:
Promise.bind(this).coroutine(function*(){…})
@robertklep:reat
Promise.coroutine(function*(){…}).调用(this)
。但是看看我的答案,找到更好的解决方案,这个解决方案也解决了
这个问题。我必须做一些额外的修改才能让它正常工作。例如,我必须保存对“this”对象的引用,并将其作为参数传递给生成器函数,因为在内部承诺时,this上下文似乎丢失了(绑定到不同的值)。我还必须将其转换为playGame()函数。我仍然不知道为什么承诺会这样做。@tutiplain
bluebird
有一种方法,您可以使用它来设置函数的
this
。比如:
Promise.bind(this).coroutine(function*(){…})
@robertklep:reat
Promise.coroutine(function*(){…}).调用(this)
。但是请看一下我的答案,以获得更好的解决方案,该解决方案也解决了
这个问题。将所有其他内容放入
中。然后
回调。尝试(在您的情况下,这只是
提示符
方法)。将所有其他内容放入
。然后
回调。嗨,这看起来比我的方法干净多了。为什么这保留了“这”的价值?我只是想理解为什么有时候它有一个不同于预期的对象。因为由
Promise.coroutine
创建的函数确实使用其
this
值调用生成器函数。嗨,这看起来比我的方法干净得多。为什么这保留了“这”的价值?我只是想理解为什么有时候它的对象与预期的不同。因为由
Promise.coroutine
创建的函数确实使用其
this
值调用生成器函数。