2016-05-10 21 views
0

私はNodeJSに新しい非常にです。ユーザーに特定の順序で入力を促すプロンプトを表示しようとしています。これは非常にずさんなコードがあるが、たとえそうであっても、私はそれがtest.displayPlayersは()前test.getType()を実行するように動作するはずだと思いますが、それ私が知っているNode.js:複数のノードが正しい順序で実行されていない

var prompt = require('prompt'); 
var promising = require("promise-adapter"); 

var promptGet = promising(prompt.get); 

prompt.start(); 

function Game() { 
    playerOneName = ''; 
    playerTwoName = ''; 
    pOneType = true; 
} 

//prompts the user for two names and assigns them 
//to player one and player to for the game object 
Game.prototype.getName = function (resolve){ 
    promptGet(['playerName1', 'playerName2']).then(function(result){ 
     this.playerOneName = result.playerName1; 
     this.playerTwoName = result.playerName2; 
    }) 
    .then(function() { 
     resolve(); 
    }); 
} 

Game.prototype.getType = function (resolve) { 
    //console.log('made it to the function'); 
    promptGet(['Player1Type']).then(function(result){ 
     if (result.Player1Type !== "X"){ this.pOneType = false;} 
     //console.log(this.pOneType); 
    }) 
    .then(function(){ 
     resolve(); 
    }); 
} 
Game.prototype.displayPlayers = function(resolve) { 
    if (pOneType === true){ 
     console.log(this.playerOneName + " will be X's"); 
     console.log(this.playerTwoName + " will be O's"); 
    } 
    else { 
     console.log(this.playerOneName + " will be O's"); 
     console.log(this.playerTwoName + " will be X's"); 
    } 
    console.log("Let's Get Started!"); 
} 

var test = new Game(); 

new Promise(function(resolve, reject){ 
     test.getName(resolve); 
    }) 
    .then(function(resolve, reject){ 
     test.getType(); 
    }) 
    .then(function(resolve, reject){ 
     test.displayPlayers(); 
    }); 

:ここに私のコードですそうではない。助けてくれてありがとう!私はコードをクリーンアップする方法についてアドバイスを受けています。

答えて

0

:それはこのような何かを行きます。

それぞれ非同期コードブロックは約束を返すべきであり、あなたのケースでは、ユーザーの入力を待っている間ブロックされます。

新しい約束または約束を返す関数を定義し、それらを互いに連鎖させることで、コードの実行順序を制御することができます。

下記の簡単なフィドルを見てください。 getNameは、約束の実装のresolverejectの機能を提供する、実行者をパラメータとするPromiseコンストラクタを使用して構成された新しい約束を返します。

非同期コードが実行された場合、またはこの場合はブロックコードでは、解決された値の有無にかかわらず、約束を解決するだけです。

デフォルトでは、.then()関数は新しい約束を返し、オブジェクトが新しい約束でない限り、.then()関数内のオブジェクトを返すと自動的に解決されます。このことから

function Game() { 
 
    var playerOneName = ''; 
 
    var playerTwoName = ''; 
 
    var pOneType = true; 
 
} 
 

 
//prompts the user for two names and assigns them 
 
//to player one and player to for the game object 
 
Game.prototype.getName = function(resolve) { 
 
    return new Promise(function(resolve, reject) { 
 
    var playerOneName = prompt("Please enter your name", "Harry Potter"); 
 
    var playerTwoName = prompt("Please enter your name", "Ron Weasley"); 
 
    if (playerOneName !== null && playerTwoName !== null) { 
 
     resolve(); 
 
    } else { 
 
     reject(); 
 
    } 
 
    }); 
 
} 
 

 
var someOtherFunction = function() { 
 
    alert("This is inside some other function!"); 
 
    return "A value that gets resolved by the promise!"; 
 
} 
 

 

 
var testGame = new Game(); 
 
testGame.getName() 
 
    .then(function() { 
 
    alert("This happens when names have been chosen!"); 
 
    }) 
 
    .then(someOtherFunction) 
 
    .then(function(resolvedValue) { 
 
    alert(resolvedValue); 
 
    })

あなたは約束の基本的なものを得ることができる必要がありますが、私は非常にあなたがMDN Promiseドキュメントを読むことをお勧めします。

0

あなたの問題は私には分かりません。私はイベントのような約束事です。

実際にtest.getType();の後にtest.displayPlayers()を実行したい場合は、そのような何かを:あなたは特定の順序でコードを実行するthenに依存することはできません

var prompt = require('prompt'); 
 
var promising = require("promise-adapter"); 
 

 
var promptGet = promising(prompt.get); 
 

 
prompt.start(); 
 

 
function Game() { 
 
    playerOneName = ''; 
 
    playerTwoName = ''; 
 
    pOneType = true; 
 
} 
 

 
//prompts the user for two names and assigns them 
 
//to player one and player to for the game object 
 
Game.prototype.getName = function (resolve){ 
 
    promptGet(['playerName1', 'playerName2']).then(function(result){ 
 
     this.playerOneName = result.playerName1; 
 
     this.playerTwoName = result.playerName2; 
 
    }) 
 
    .then(function() { 
 
     resolve(); 
 
    }); 
 
} 
 

 
Game.prototype.getType = function (resolve) { 
 
    //console.log('made it to the function'); 
 
    promptGet(['Player1Type']).then(function(result){ 
 
     if (result.Player1Type !== "X"){ this.pOneType = false;} 
 
     //console.log(this.pOneType); 
 
    }) 
 
    .then(function(){ 
 
     resolve(); 
 
    }); 
 
} 
 
Game.prototype.displayPlayers = function(resolve) { 
 
    if (pOneType === true){ 
 
     console.log(this.playerOneName + " will be X's"); 
 
     console.log(this.playerTwoName + " will be O's"); 
 
    } 
 
    else { 
 
     console.log(this.playerOneName + " will be O's"); 
 
     console.log(this.playerTwoName + " will be X's"); 
 
    } 
 
    console.log("Let's Get Started!"); 
 
} 
 

 
var test = new Game(); 
 

 
new Promise(function(resolve, reject){ 
 
     test.getName(resolve); 
 
    }) 
 
    .then(function(resolve, reject){ 
 
     test.getType(); 
 
     test.displayPlayers(); 
 
    })

+0

しかし、これを行うと、displayType()が実行される前にdisplayPlayers()が実行され、結果が未定義になります。 – cbrauns

1

thenは非同期関数です。 一連の約定を特定の順序で実行する場合は、Async.jsをご覧ください。私は約束はこれを行う方法ですが、あなたはまだそれを使用したい場合、あなたは手元の作業のためにそれらを使用する方法を誤解したことを言わないだろう

async.series([ 
    function(){ ... }, 
    function(){ ... } 
]); 
+0

さて、私は間違いなく、しかし、私は、次のものが実行される前に、それまでの完了まで待つことが重要だと考えましたか? – cbrauns

+0

は、同じ時刻に複数のスレッドを実行していると思いますが、どちらのスレッドを先に開始するのが確実ですが、他のスレッドはお互いが実行を開始するのを待つ必要はありません –

関連する問題