私は少し失われています。私は "樹木症候群のための木材を見ることができません"と打っています。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
イムが失わ..
感謝を事前にコードの最後の部分は意図しないバグを持っている
あなたは小さなタイプミスがあります。 –