我试图进行后端调用以获取一些数据并从中进行第二次调用。后端调用完美,但唯一的问题是承诺。我无法从第一个承诺获得数据,所以我可以从它进行第二次调用.. 我做错了什么想法?
login() {
if (this.formLogin.valid) {
var data;
this.userService.performRequestLoginToBackend(this.formLogin.value.username, this.formLogin.value.password).then(response => {
data = response;
// this.userStore.dispatch({type: 'USER_LOGIN', data: this.userData});
}, (response) => {
if (response.status < 500) {
console.warn('Login failed!');
} else {
console.error('Internal Server Error');
}
});
this.userService.performSecondRequestLogin(data).then(response2 => {
this.parsePromise(response2);
}, (response2) => {
if (response2.status < 500) {
console.warn('Login failed!');
} else {
console.error('Internal Server Error');
}
});
}
}
我试过的第二种方式:
login() {
if (this.formLogin.valid) {
this.userService.performRequestLoginToBackend(this.formLogin.value.username, this.formLogin.value.password).then(response => {
this.userService.performSecondRequestLogin(response).then(response2 => {
this.parsePromise(response2);
}, (response2) => {
if (response2.status < 500) {
console.warn('Login failed!');
} else {
console.error('Internal Server Error');
}
});
// this.userStore.dispatch({type: 'USER_LOGIN', data: this.userData});
}, (response) => {
if (response.status < 500) {
console.warn('Login failed!');
} else {
console.error('Internal Server Error');
}
});
}
}
更新:仍然无法运作......
login() {
if (this.formLogin.valid) {
this.userService.performRequestLoginToBackend(this.formLogin.value.username, this.formLogin.value.password).then(response => {
return this.userService.performSecondRequestLogin(response);
// this.userStore.dispatch({type: 'USER_LOGIN', data: this.userData});
}, (response) => {
if (response.status < 500) {
console.warn('Login failed!');
} else {
console.error('Internal Server Error');
}
}).then(response => {
this.parsePromise(response);
},(response) => {
if (response.status < 500) {
console.warn('Login failed!');
} else {
console.error('Internal Server Error');
}
});
}
}
后端呼叫第一次呼叫
performRequestLoginToBackend(_username: string, _password: string) {
let promise = new Promise((resolve, reject) => {
this.http.get('xxx').subscribe(data => {
});
});
return promise;
}
答案 0 :(得分:2)
您可以使用以下链接承诺。在这种情况下,最后的then
回调,响应是第二次调用的响应。
this.userService.performRequestLoginToBackend(
this.formLogin.value.username,
this.formLogin.value.password).then(response => {
data = response;
return this.userService.performSecondRequestLogin(data); // <-----
}, (response) => {
if (response.status < 500) {
console.warn('Login failed!');
} else {
console.error('Internal Server Error');
}
}).then(response => {
(...)
});
答案 1 :(得分:0)
我认为@Thierry Templier的答案几乎就在那里,但你需要正确地平衡承诺链:
this.userService.performRequestLoginToBackend(
this.formLogin.value.username,
this.formLogin.value.password).then((response) => {
return this.userService.performSecondRequestLogin(response); // <-----
})
// Flattened properly here, assuming performSecondRequestLogin
// returns a promise
.then((response) => {
if (response.status < 500) {
console.warn('Login failed!');
} else {
console.error('Internal Server Error');
}
})
// Catch for good measure!
.catch((error)=>{...});