使用异步等待的顺序函数调用问题
Issue with Sequential function calls using async await
我发现当我尝试调用函数时需要先调用 initializeItems()。但是 checkList() 在 initializeItems()
之前被调用
initializeItems() {
this.dataService.readLocalData().subscribe( data => {
console.log('Local Data');
this.items = data;
// console.log(this.items);
this.tempItems = [];
for (const i of this.items.material ){
console.log(i['material-name']);
this.tempItems.push( i['material-name'] );
}
console.log('***********************************************');
console.log(this.tempItems);
});
}
checkList(ev: any){
// set val to the value of the searchbar
const val = ev.target.value;
console.log(val);
console.log(this.tempItems);
// if the value is an empty string don't filter the items
if (val && val.trim() !== '') {
this.tempItems = this.tempItems.filter((item) => {
return (item.toLowerCase().indexOf(val.toLowerCase()) > -1);
});
}
}
async getItems(ev: any) {
// Reset items back to all of the items
await this.initializeItems(); //This need to execute first
await this.checkList(ev); //But this getting executed
}
如果函数顺序调用。我的结果将是
初始化项目()
变量 tempItems 将是//完整列表
然后
检查列表()
变量 tempItems 将是//来自可搜索下拉列表的过滤列表
await
用于承诺。由于 initializeItems 没有 return 承诺,因此 await 实际上并不等待任何东西。您需要将 initializeItems 修改为 return 一个承诺。我的猜测是您希望订阅回调只被调用一次,并且在那时承诺应该解决,所以您想要这样做:
initializeItems() {
return new Promise((resolve) => { // <---- creating a promise
this.dataService.readLocalData().subscribe( data => {
console.log('Local Data');
this.items = data;
// console.log(this.items);
this.tempItems = [];
for (const i of this.items.material ){
console.log(i['material-name']);
this.tempItems.push( i['material-name'] );
}
console.log('***********************************************');
console.log(this.tempItems);
resolve(); // <-- resolving the promise
});
}
}
我发现当我尝试调用函数时需要先调用 initializeItems()。但是 checkList() 在 initializeItems()
之前被调用 initializeItems() {
this.dataService.readLocalData().subscribe( data => {
console.log('Local Data');
this.items = data;
// console.log(this.items);
this.tempItems = [];
for (const i of this.items.material ){
console.log(i['material-name']);
this.tempItems.push( i['material-name'] );
}
console.log('***********************************************');
console.log(this.tempItems);
});
}
checkList(ev: any){
// set val to the value of the searchbar
const val = ev.target.value;
console.log(val);
console.log(this.tempItems);
// if the value is an empty string don't filter the items
if (val && val.trim() !== '') {
this.tempItems = this.tempItems.filter((item) => {
return (item.toLowerCase().indexOf(val.toLowerCase()) > -1);
});
}
}
async getItems(ev: any) {
// Reset items back to all of the items
await this.initializeItems(); //This need to execute first
await this.checkList(ev); //But this getting executed
}
如果函数顺序调用。我的结果将是
初始化项目()
变量 tempItems 将是//完整列表
然后
检查列表()
变量 tempItems 将是//来自可搜索下拉列表的过滤列表
await
用于承诺。由于 initializeItems 没有 return 承诺,因此 await 实际上并不等待任何东西。您需要将 initializeItems 修改为 return 一个承诺。我的猜测是您希望订阅回调只被调用一次,并且在那时承诺应该解决,所以您想要这样做:
initializeItems() {
return new Promise((resolve) => { // <---- creating a promise
this.dataService.readLocalData().subscribe( data => {
console.log('Local Data');
this.items = data;
// console.log(this.items);
this.tempItems = [];
for (const i of this.items.material ){
console.log(i['material-name']);
this.tempItems.push( i['material-name'] );
}
console.log('***********************************************');
console.log(this.tempItems);
resolve(); // <-- resolving the promise
});
}
}