gpt4 book ai didi

javascript - Bluebird 协程根本不执行

转载 作者:行者123 更新时间:2023-11-30 09:56:27 26 4
gpt4 key购买 nike

我正在尝试实现剪刀石头布游戏的 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() 函数返回使用 new Promise() 做出的 promise 。如果我这样做:

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

promise 正确执行。但是,当在 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 是一个高阶函数,即它接受一个生成器函数并返回另一个函数,该函数在被调用时将返回您正在寻找的 promise 。正如@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');
}
});

关于javascript - Bluebird 协程根本不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33712477/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com