2017-06-28 21 views
0

I持っているいくつかの理由で「whileループに入る」から「JOBSTARTコールの後コンポーネントラインを提出」印刷から直進するこの機能:たsetIntervalは決して機能に発射されていないと知っていない理由(角度)

returned() { 
    this.numReturned++; 
    if (this.numReturned !== this.numSubmitting) { 
     return; 
    } 
    console.log('submit component line before jobstart call'); 
    this.submitService.startJobToAuditTable().subscribe(); 
    console.log('submit component line after jobstart call'); 

    let timer = setInterval(() => { 
     console.log('start of interval'); 
     this.submitService.checkIfJobRunning().subscribe(res => { 
      console.log("res from job running check is " + res); 
      this.jobFinished = res; 
     }); 

     this.submitService.pollAuditTable().subscribe(res => { 
      console.log("res from audit table:\n\t" + res); 
      this.auditRows = res; 
     }); 
    }, 10000); //runs every 10 seconds. will be longer after testing 

    console.log('entering while loop'); 
    while (this.jobFinished === false) { 

    } 

    console.log('clearing interval, job should be finished now'); 
    clearInterval(timer); 
    return; 
} 

console.logと他のものが呼び出されることはありません。その理由はわかりません。

+0

その非同期呼び出し、あなたのサービスは、すべての作業ですあなたはよくチェックしました –

+1

あなたの無限ループがすべてをブロックしています。それをしないでください。 –

+1

setIntervalは、指定された関数への非同期呼び出しを作成します。その前後にある行の間には実行が一時停止しません。他のコンソールログは10秒後に表示されます(あなたの10000引数ごとに)。しかし、torazaburoが言及しているように、あなたは無限ループを起こしているので、何か他のものがブロックされてしまっても決して表示されないようになります。 –

答えて

1

あなたのwhileループは実行をすべて消費し、すべてをブロックしています。あなたは仕事はあなたが約束を返すことができますwhile(done){}ループの代わりに完了するまで待ちたい場合:

returned() { 
    return new Promise((resolve, reject) => { 
     this.numReturned++; 
     if (this.numReturned !== this.numSubmitting) { 
      resolve(); 
      return; 
     } 
     console.log('submit component line before jobstart call'); 
     this.submitService.startJobToAuditTable().subscribe(); 
     console.log('submit component line after jobstart call'); 

     let timer = setInterval(() => { 

      console.log('start of interval'); 
      this.submitService.checkIfJobRunning().subscribe(res => { 
       console.log("res from job running check is " + res); 
       this.jobFinished = res; 
      }); 

      this.submitService.pollAuditTable().subscribe(res => { 
       console.log("res from audit table:\n\t" + res); 
       this.auditRows = res; 
      }); 

-->  if(this.jobFinished === false) { 
       console.log('clearing interval, job should be finished now'); 
       clearInterval(timer); 
       resolve(); 
      } 

     }, 10000); //runs every 10 seconds. will be longer after testing 

    } 
} 

resolve()

は非同期コードでは、代わりに returnの使用されています。あなたは待つしたい場合は、この関数を呼び出すときに、ジョブが終了するまで

後で、awaitキーワードを使用することができます

await returned(); // will wait until resolve() is called 
// code after all jobs done 

場合、または非同期/好ましくない待つ

returned().then(() => { 
    // code after all jobs done 
}) 
0

削除このコードはブロックしているので、プロセスが次のティックをトリガすることはできません。したがって、setIntervalを含む何も起動しません。

// Delete this 
while (this.jobFinished === false) { } 

次に、あなたが私はあなたが正しく何をしようとして理解していれば、あなたはこのような何かを行うことができ、よくてclearInterval

this.submitService.checkIfJobRunning().subscribe(res => { 
    console.log("res from job running check is " + res); 
    this.jobFinished = res; 
    if(this.jobFinished !== false){ 
    clearInterval(timer); 
    } 
}); 
0

を移動することができます。

returned() { 
    this.numReturned++; 
    if (this.numReturned !== this.numSubmitting) { 
     return resolve(); 
    } 
    console.log('submit component line before jobstart call'); 
    this.submitService.startJobToAuditTable().subscribe(); 
    console.log('submit component line after jobstart call'); 

    let timer = setInterval(() => { 
     console.log('start of interval'); 
     this.submitService.checkIfJobRunning().subscribe(res => { 
      console.log("res from job running check is " + res); 
      this.jobFinished = res; 

      if (this.jobFinished) { 
       console.log('clearing interval, job should be finished now'); 
       clearInterval(timer); 
      } 
     }); 

     this.submitService.pollAuditTable().subscribe(res => { 
      console.log("res from audit table:\n\t" + res); 
      this.auditRows = res; 
     }); 
    }, 10000); //runs every 10 seconds. will be longer after testing 

    console.log('entering wait phase'); 
} 

しかし、ジョブが終了したときに関数が戻りたいと思う場合は、約束を返し、実行スコープをasync

と宣言する必要があります
returned() { 
    return new Promise((resolve) => { 
     this.numReturned++; 
     if (this.numReturned !== this.numSubmitting) { 
      return resolve(); 
     } 
     console.log('submit component line before jobstart call'); 
     this.submitService.startJobToAuditTable().subscribe(); 
     console.log('submit component line after jobstart call'); 

     let timer = setInterval(() => { 
      console.log('start of interval'); 
      this.submitService.checkIfJobRunning().subscribe(res => { 
       console.log("res from job running check is " + res); 
       this.jobFinished = res; 

       if (this.jobFinished) { 
        console.log('clearing interval, job should be finished now'); 
        clearInterval(timer); 

        resolve(); 
       } 
      }); 

      this.submitService.pollAuditTable().subscribe(res => { 
       console.log("res from audit table:\n\t" + res); 
       this.auditRows = res; 
      }); 
     }, 10000); //runs every 10 seconds. will be longer after testing 

     console.log('entering wait phase'); 
    }) 
} 

そして、どのようにそれを呼び出すために:

async someOtherMemberMethod() { 
    await this.returned(); 
    // job is finished here 
} 

か、async機能に入れたくない場合は:

someOtherMemberMethod() { 
    this.returned().then(() => { 
     // job is finished here 
    }); 
}