我的服务类别如下
import { RetrieveMultipleResponse } from 'xrm-webapi/dist/models';
@Injectable()
export class CrmService {
headers: any;
error : any;
store : Observable<any>;
constructor(public crmDataService : CrmDataService) {}
retrieveAllTeams(): Promise<RetrieveMultipleResponse> {
return retrieveMultiple(this.crmDataService.config,"teams",null);
}
}
我在这样的组件中调用它:
load() {
this.crmService.retrieveAllTeams()
.then((results) => {
if(results.value !== undefined && results.value !== null) {
var teams = results.value;
}
},
(error) => {
});
}
因此retrieveAllTeams
返回类型为RetrieveMultipleResponse
的Promise。界面如下所示:
export interface RetrieveMultipleResponse {
value: Entity[];
'@odata.nextlink': string;
}
我想避免在组件中使用嵌套的Promise,所以我使用的是Angular6。我尝试使用async
和await
,但是我真的不知道如何获得结果以及组件中retrieveAllTeams
方法的错误。
答案 0 :(得分:2)
我想避免在组件中使用嵌套的Promise,所以我使用的是Angular 6,我尝试使用async和await,但无法真正弄清楚如何从retrieveAllTeams中获取结果和错误
您可以通过以下方式做到这一点:
async load() {
// use try-catch block for handling errors
try {
// const result will have results from the
// retrieveAllTeams
const result = await this.crmService.retrieveAllTeams();
if (results.value !== undefined && results.value !== null) {
var teams = results.value;
}
} catch (err) {
// catch errors if any
console.log(err);
}
}