2016-12-30 7 views
1

私は少し失われています。私は "樹木症候群のための木材を見ることができません"と打っています。JavaScript/Node約束:それらを順番に実行

Im JS NooBとImは、(約束を返す)一連のJS関数を順番に呼び出す方法を理解しようとしています。アイブ氏は、私がうまくカント何

は..ですいくつかは、アップ読んで行われ、イムは、私は約束を管理するために、青い鳥のようなものを使うべきノードを使用して、指定されたと判断した理由コードのdoesntの仕事のこの作品

var Promise = require("bluebird"); 
// My Promise enabled function from oReily Safari book 
function countdown(seconds, timername) { 
    return new Promise(function (resolve, reject) { 
     console.log('Countdown : Starting Countdown ' + timername); 
     for (let i = seconds; i >= 0; i--) { 
      setTimeout(function() { 
       if (i > 0) 
        console.log(timername + ' ' + i + '...'); 
       else 
       { 
        console.log('countdown '+timername+' now=='+i+' resolving!'); 
        resolve(console.log("Countdown : timename="+timername+" ended")); 
       } 
      }, (seconds - i) * 1000); 
     } 
    }); 
} 

/*Basic test of promise */ 
/* when this is run I expected countdown(5, 'Basic : Timer1') to execute and then when **resolved** THEN countdown(5, "Basic Timer2") to execute. 
*however what I see is both timers executing at the same time.. 
*/ 

console.log('Basic : Countdown promise test'); 
countdown(5, 'Basic : Timer1'). 
     then(function() 
     { 
      /*Success */ 
      console.log("Basic : Ended Successfully"); 
     }). 
     then(countdown(5, "Basic : Timer2") 
       ); 

とき私はこれを実行したときしかし、私は

Basic : Countdown promise test 
Countdown : Starting Countdown Basic : Timer1 
Countdown : Starting Countdown Basic : Timer2 
Basic : Timer1 5... 
Basic : Timer2 5... 
Basic : Timer1 4... 
Basic : Timer2 4... 
Basic : Timer1 3... 
Basic : Timer2 3... 
Basic : Timer1 2... 
Basic : Timer2 2... 
Basic : Timer1 1... 
Basic : Timer2 1... 
countdown Basic : Timer1 now==0 resolving! 
Countdown : timename=Basic : Timer1 ended 
countdown Basic : Timer2 now==0 resolving! 
Countdown : timename=Basic : Timer2 ended 
Basic : Ended Successfully 
Done. 
を取得

..私はカウントダウン(5「タイマ1」)最初に実行すると、その後、唯一のタイマ1が終了したときに、実行されますが、タイマ2になると期待しています、これを実行します210

イムが失わ..

感謝を事前にコードの最後の部分は意図しないバグを持っている

+0

あなたは小さなタイプミスがあります。 –

答えて

4

に:

then(countdown(5, "Basic : Timer2")); 

これは、コールバック関数として使用されている()カウントダウンの結果を意味しています。 (カウントダウン機能を直接実行される)代わり

then(function(lastResult){ countdown(5, "Basic : Timer2") }); 

を使用

可変lastResultは、鎖中に、以前の約束から返された値を有することになります。あなたはこのプロジェクトを試すことができます

+0

ありがとうございました!特に「ありがとう」理由を説明してくれてありがとうございました。「then」はコールバック関数、関数呼び出し自体はパラメータではありません。 .. – italianknows

1
console.log('Basic : Countdown promise test'); 
countdown(5, 'Basic : Timer1'). 
     then(function() 
      { 
       /*Success */ 
       console.log("Basic : Ended Successfully"); 
       return countdown(5, "Basic : Timer2"); 
      }). 
      then(function(){ 
       console.log("Finish!"); 
      }); 
0

https://github.com/LvChengbin/sequence

Sequence.all([ 
    () => countdown(5, 'Basic : Timer1'), 
    () => countdown(5, 'Basic : Timer2') 
]) 

そして、あなたはinterval 2ごとの間のステップを指定するSequence.allメソッドの第2引数を使用することができます。

詳しくは、ドキュメントを参照してください。

関連する問題