【发布时间】:2021-09-23 08:19:44
【问题描述】:
我有一个异步/等待功能。我应该如何重写它以保持异步但不使用异步/等待?我想使用promises,但是当我想重写这段代码时,它不起作用......:/
fruit = 'apple';
data: any = {};
constructor(private http: HttpClient, private storage: Storage) {
}
async function(ex: string): Promise<{}> {
ex = ex ? ex : await this.storage.get('ex');
return new Promise<{}>((resolve) => {
const path = `./exam/${ex || 'apple'}.json`;
this.fruit = ex;
this.storage.set('ex', this.fruit );
this.http.get<{}>(path).subscribe(
transition => {
this.data = Object.assign({}, transition || {});
return resolve(this.data);
},
() => {
this.data = {};
return resolve(this.data);
}
);
});
}
get(): string {
return this.fruit;
}
}
【问题讨论】:
标签: angular asynchronous async-await promise