如何在承诺后调用函数
How to call a function after promise
JavaScript 菜鸟在这里。缺乏对重要设计模式的了解。
this.getEventData(); < 我怎样才能在 promise 解决后调用那个函数?那么我可以删除 noobie setTimeout ?
public onInit(): Promise<void> {
return super.onInit().then(_ => {
new Promise<void>((resolve: () => void, reject: (error: any) => void): void => {
this.context.aadHttpClientFactory
.getClient(APIAppRegID)
.then((client: AadHttpClient): void => {
this.httpClient = client;
resolve();
}, err => reject(err));
});
setTimeout(() => {
this.getEventData();
}, 1500);
});
}
承诺没有任何意义。你没有使用它。您可以删除它:
public onInit(): Promise<void> {
return super.onInit().then(_ => {
this.context.aadHttpClientFactory
.getClient(APIAppRegID)
.then((client: AadHttpClient): void => {
this.httpClient = client;
this.getEventData();
});
});
}
async/await
版本:
async onInit(): Promise<void> {
await super.onInit();
this.httpClient = await this.context.aadHttpClientFactory.getClient(APIAppRegID);
this.getEventData();
}
JavaScript 菜鸟在这里。缺乏对重要设计模式的了解。
this.getEventData(); < 我怎样才能在 promise 解决后调用那个函数?那么我可以删除 noobie setTimeout ?
public onInit(): Promise<void> {
return super.onInit().then(_ => {
new Promise<void>((resolve: () => void, reject: (error: any) => void): void => {
this.context.aadHttpClientFactory
.getClient(APIAppRegID)
.then((client: AadHttpClient): void => {
this.httpClient = client;
resolve();
}, err => reject(err));
});
setTimeout(() => {
this.getEventData();
}, 1500);
});
}
承诺没有任何意义。你没有使用它。您可以删除它:
public onInit(): Promise<void> {
return super.onInit().then(_ => {
this.context.aadHttpClientFactory
.getClient(APIAppRegID)
.then((client: AadHttpClient): void => {
this.httpClient = client;
this.getEventData();
});
});
}
async/await
版本:
async onInit(): Promise<void> {
await super.onInit();
this.httpClient = await this.context.aadHttpClientFactory.getClient(APIAppRegID);
this.getEventData();
}