2017-07-05 4 views
1

配列に問題があります。変更してforeachで再度使用する必要があります。ここでは、コードの一部です:配列が別の関数によって変更される前に角2のforeachが実行される

this.selectedDepartementen.forEach(element => { 
     this.DepID = element.ID; 
     if (this.USERSDepIDs.indexOf(this.DepID) == -1) { 
     this.insertDepartementCoupling(this.DepID, this.USERid, this.AJID, res => { 
      if (res) { 
      this.loading = false; 
      return; 
      } 
      this.removeAllFromArray(this.USERSDepIDs, this.DepID, res => { 
      this.USERSDepIDs = res; 
      }) 
     }) 
     } else 
     { 
     this.updateDepartementCoupling(this.DepID, this.USERid, this.AJID, res => { 
      if (res) { 
      this.loading = false; 
      return; 
      } 
      this.removeAllFromArray(this.USERSDepIDs, this.DepID, res => { 
      this.USERSDepIDs = res; 
      }) 
     }) 
     } 
    }); 

あなたは、私が最後に使用DepIDを削除する機能removeAllFromArrayを呼び出しています見ることができるように。しかしforeachはその機能を待つことはなく、そのまま続ける。私はこれが非同期のものだと推測していますが、どうすれば修正できますか?

ありがとうございます!ユーザダンカン機能insertDepartementCoupling追加

EDIT

async insertDepartementCoupling(DepID, USERid, AJID, callback) { 
    var test = await this.departementService.insertDepartementCoupling(DepID, USERid, AJID).subscribe(
     data => this.mockdata = data, 
     error => { 
     this.showMessage("error", "Departement koppeling niet gelukt!", "Departement koppeling niet gelukt, zie console voor meer informatie."); 
     callback(true); 
     }, 
    () => { 
     if (this.mockdata._body) { 
      this.showMessage("error", "Departement koppeling niet gelukt!", "Contacteer de administrator!"); 
      callback(true); 
     } else { 
      this.showMessage("succes", "Departement koppeling geslaagd!", "Gebruiker is gekoppeld aan de departement(en)."); 
      callback(false); 
     } 
     }); 
    } 
+0

使用している他の機能を書き換えることはできますか?彼らはコールバックを使用しているので、すべてが合理的な順序で行われることを確実にすることは非常に困難です。代わりに約束を返すようにそれらを変更することができれば、コードを単純化してasync/awaitを使用してコードを平坦化することができます。 – Duncan

+0

私はすべてを書き換えることができます。私はasyncをどのように使うことができるか/コードを平坦化するのを待っているかの小さな例を教えてください。 –

答えて

1

あなたはinsertDepartmentCouplingupdateDepartementCouplingを変更した場合ので、その代わりに、コールバックの彼らは、コールバックに渡されていたであろう結果と解決の約束を返し、あなたも(removeAllFromArrayのために同じことを行うには、その1つの必要性を行います?typescriptですとあなたは、配列を反復処理するのではなくするためにfor..ofを使用できることを

for (const element of this.selectedDepartementen) { 
    this.DepID = element.ID; 
    if (this.USERSDepIDs.indexOf(this.DepID) == -1) { 
     if (await this.insertDepartementCoupling(this.DepID, this.USERid, this.AJID)) { 
      this.loading = false; 
     } else { 
      this.USERSDepIDs = await this.removeAllFromArray(this.USERSDepIDs, this.DepID); 
     } 
    } else { 
     if (await this.updateDepartementCoupling(this.DepID, this.USERid, this.AJID)) { 
      this.loading = false; 
     } else { 
      this.USERSDepIDs = await this.removeAllFromArray(this.USERSDepIDs, this.DepID); 
     } 
    } 
} 

注:すべてで非同期に)、その後、あなたはasyncを投稿コードが含まれている機能を作る、そしてあなたは、このようなコードを使用することができますforEachと避けてください別のコールバック。

このコードの実行方法ははっきりしています。特に、ループではすべてが完了するのを待っていますが、まだ完全に非同期です。それを含む関数はasyncキーワードで宣言し、Promiseを返す必要があります。awaitか、.then()コールバックで処理できます。

insertDepartementCoupling関数はすでに非同期であり、オブザーバを内部的に使用しているため、コールバックを削除してプロミスを返す最も簡単な方法は、オブザーバブルにtoPromise()を使用することです。その約束は、最後のデータ値で完了しますが、あなたは、あなたが戻りたいブール値を取得するために.then()を使用することができます。また、あなたが早く約束にObservableを変換することができ、その後、あなたは中間体を必要としない

async insertDepartementCoupling(DepID, USERid, AJID): Promise<boolean> { 
    let result = false; 
    var test = await this.departementService.insertDepartementCoupling(DepID, USERid, AJID).subscribe(
     data => this.mockdata = data, 
     error => { 
     this.showMessage("error", "Departement koppeling niet gelukt!", "Departement koppeling niet gelukt, zie console voor meer informatie."); 
     result = true; 
     }, 
    () => { 
     if (this.mockdata._body) { 
      this.showMessage("error", "Departement koppeling niet gelukt!", "Contacteer de administrator!"); 
      result = true; 
     } else { 
      this.showMessage("succes", "Departement koppeling geslaagd!", "Gebruiker is gekoppeld aan de departement(en)."); 
      result = false; 
     } 
     }).toPromise(); 
    return result; 
    } 

を変数:

async insertDepartementCoupling(DepID, USERid, AJID): Promise<boolean> { 
    return this.departementService.insertDepartementCoupling(DepID, USERid, AJID).toPromise() 
    .then(
     data => { 
     this.mockdata = data 
     if (this.mockdata._body) { 
      this.showMessage("error", "Departement koppeling niet gelukt!", "Contacteer de administrator!"); 
      return true; 
     } else { 
      this.showMessage("succes", "Departement koppeling geslaagd!", "Gebruiker is gekoppeld aan de departement(en)."); 
      return false; 
     } 
     }, 
     error => { 
     this.showMessage("error", "Departement koppeling niet gelukt!", "Departement koppeling niet gelukt, zie console voor meer informatie."); 
     return true; 
     }); 
    } 
+0

ありがとうございました!返された約束は本当か偽かを含むだけで、私はこれをどのように作成できるのか分かりません。 'insertDepartmentCoupling'関数を追加して、何が起こっているのかを確認します。 –

+0

あなたは素晴らしいです!私はすべてのコールバック関数でこれを使うことができます!どうもありがとうございます! –

関連する問題