Angular 8 异步调用未等待 promise 完成且结果为错误请求
Angular 8 Async call not waiting for promise to complete and results as bad request
我希望我的异步调用在完成之前不要转到下一个语句,但它仍在继续并且在请求完成后它会导致错误作为错误请求,即使 url 路径和 http headers 是 correct.if 我使用 observable 和 subscribe 的工作方式来完成它但是在完成请求之前转到下一个语句,所以我认为 promise async/await 可以帮助但是我做错了什么,任何帮助是非常可观。这是我的代码。
myapp.service.ts
//Because we are not using a type checker, so the response should be extracted. Add this function after the constructor for extract it.
private extractData(res: Response) {
let body = res;
return body || {};
}
private handleError(error) {
return throwError(error);
}
async getEntityDependencies(entitytype: string) {
try {
var authheader = { headers: new HttpHeaders({ 'accesstoken': this.accesstoken, 'Content-Type': 'application/json', })};
return await this.http.get(this.endpoint, authheader).pipe(map(this.extractData), catchError(this.handleError)).toPromise();
}
catch (error) {
console.log(error);
}
}
newform.component.ts
async CreateForm() {
// const data = await this.rest.getEntityDependencies(this.dataservice.entitytype)
await this.rest.getEntityDependencies(this.dataservice.entitytype).then((data: {}) => { // data is getting undefined since request results Bad request
// my stuff using data...
}, error => {
console.log(error, "Error from API");
});
}
代码中有两个问题
首先你不需要 return await
async getEntityDependencies(entitytype: string) {
try {
var authheader = { headers: new HttpHeaders({ 'accesstoken': this.accesstoken, 'Content-Type': 'application/json', })};
return this.http.get(this.endpoint, authheader)
.pipe(
map(this.extractData),
tap(data => console.log('data in service', data))
catchError(this.handleError)
).toPromise();
}
catch (error) {
console.log(error);
}
}
第二种方法
async CreateForm() {
try
{
const data = await this.rest.getEntityDependencies(this.dataservice.entitytype);
console.log('data', data);
}
catch(ex) {
console.error(ex);
}
}
我希望我的异步调用在完成之前不要转到下一个语句,但它仍在继续并且在请求完成后它会导致错误作为错误请求,即使 url 路径和 http headers 是 correct.if 我使用 observable 和 subscribe 的工作方式来完成它但是在完成请求之前转到下一个语句,所以我认为 promise async/await 可以帮助但是我做错了什么,任何帮助是非常可观。这是我的代码。
myapp.service.ts
//Because we are not using a type checker, so the response should be extracted. Add this function after the constructor for extract it.
private extractData(res: Response) {
let body = res;
return body || {};
}
private handleError(error) {
return throwError(error);
}
async getEntityDependencies(entitytype: string) {
try {
var authheader = { headers: new HttpHeaders({ 'accesstoken': this.accesstoken, 'Content-Type': 'application/json', })};
return await this.http.get(this.endpoint, authheader).pipe(map(this.extractData), catchError(this.handleError)).toPromise();
}
catch (error) {
console.log(error);
}
}
newform.component.ts
async CreateForm() {
// const data = await this.rest.getEntityDependencies(this.dataservice.entitytype)
await this.rest.getEntityDependencies(this.dataservice.entitytype).then((data: {}) => { // data is getting undefined since request results Bad request
// my stuff using data...
}, error => {
console.log(error, "Error from API");
});
}
代码中有两个问题
首先你不需要 return await
async getEntityDependencies(entitytype: string) {
try {
var authheader = { headers: new HttpHeaders({ 'accesstoken': this.accesstoken, 'Content-Type': 'application/json', })};
return this.http.get(this.endpoint, authheader)
.pipe(
map(this.extractData),
tap(data => console.log('data in service', data))
catchError(this.handleError)
).toPromise();
}
catch (error) {
console.log(error);
}
}
第二种方法
async CreateForm() {
try
{
const data = await this.rest.getEntityDependencies(this.dataservice.entitytype);
console.log('data', data);
}
catch(ex) {
console.error(ex);
}
}