配列に問題があります。変更して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);
}
});
}
使用している他の機能を書き換えることはできますか?彼らはコールバックを使用しているので、すべてが合理的な順序で行われることを確実にすることは非常に困難です。代わりに約束を返すようにそれらを変更することができれば、コードを単純化してasync/awaitを使用してコードを平坦化することができます。 – Duncan
私はすべてを書き換えることができます。私はasyncをどのように使うことができるか/コードを平坦化するのを待っているかの小さな例を教えてください。 –