Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 等待来自HttpClient get的响应-同步调用_Javascript_Angular_Typescript_Angular Httpclient - Fatal编程技术网

Javascript 等待来自HttpClient get的响应-同步调用

Javascript 等待来自HttpClient get的响应-同步调用,javascript,angular,typescript,angular-httpclient,Javascript,Angular,Typescript,Angular Httpclient,我想知道如何等待调用后端的HttpClient操作的响应,我需要等待后端返回响应(同步调用),经典方法是订阅可观察的响应: //My component ... console.log("before"); this.myService.getSomeThingFromBackend('param_value').subscribe(response => { console.log("do my stuff"); } console.log("after"); //My Se

我想知道如何等待调用后端的HttpClient操作的响应,我需要等待后端返回响应(同步调用),经典方法是订阅可观察的响应:

//My component
...
console.log("before");
this.myService.getSomeThingFromBackend('param_value').subscribe(response => {
     console.log("do my stuff");
}
console.log("after");

//My Service
...
getSomeThingFromBackend(param: string): Observable<any>{
        return this.httpClient.get(this.host + this.url+ "/" + param);
}
我想要这个结果:

before

do my stuff

after

因为听起来风险很明显。。。如果您想一个接一个地执行一个操作,请将该操作移动到错误的位置

//我的组件
...
控制台日志(“之前”);
this.myService.getSomeThingFromBackend('param_value')。订阅(响应=>{
log(“做我的事”);
控制台日志(“之后”);
}
//我的服务
...
getSomeThingFromBackend(参数:字符串):可观察{
返回this.httpClient.get(this.host+this.url+“/”+param);
}

您正在做的其他一切都是合理的。您正在从服务返回可观察对象,在组件中订阅它,并在订阅中运行操作。

此过程异步工作,因此您不能让它一直等待其他内容,直到它们在响应范围内。您可以在同一范围或mak中编写这些调用创建一个函数并从内部调用它

//My component

...
console.log("before");
this.myService.getSomeThingFromBackend('param_value').subscribe(response => {
     console.log("do my stuff");
     doSomeStuff();
}

doSomeStuff(){
    console.log("after");
}

//My Service
...
getSomeThingFromBackend(param: string): Observable<any>{
        return this.httpClient.get(this.host + this.url+ "/" + param);
}
//我的组件
...
控制台日志(“之前”);
this.myService.getSomeThingFromBackend('param_value')。订阅(响应=>{
log(“做我的事”);
doSomeStuff();
}
doSomeStuff(){
控制台日志(“之后”);
}
//我的服务
...
getSomeThingFromBackend(参数:字符串):可观察{
返回this.httpClient.get(this.host+this.url+“/”+param);
}

Angular的
this.http.get
返回一个RxJS Observable。然后调用
this.http.get(…).subscribe(…)
返回RxJS
Subscription
对象。因此它们都不返回承诺,因此您不能将它们与
wait
一起使用

如果您希望能够对可观察对象使用
wait
,则必须使用
toPromise()
而不是
subscribe()
,它返回一个承诺,该承诺由该可观察对象发出的第一个值解析(它在内部为您调用
subscribe
,并用承诺对象包装)


只需放入
console.log(“之后”)
订阅内部,因为它是一个异步过程。我看到很多人建议通过将可观察问题转换为承诺来解决问题。不!这只是意味着可观察问题被错误地使用。使用承诺意味着你失去了RxJS的很多功能。@KurtHamilton我需要同步地进行调用,一个ny way感谢您的澄清,这是100%correct@SanjayLohar我要补充的是,这个承诺应该用then()end catch()methods来处理。你可以给出一个正确的语法,这样其他人就可以用标准的方法来使用它了。@JacopoSciampi编辑的问题,我想已经够清楚了
//My component

...
console.log("before");
this.myService.getSomeThingFromBackend('param_value').subscribe(response => {
     console.log("do my stuff");
     doSomeStuff();
}

doSomeStuff(){
    console.log("after");
}

//My Service
...
getSomeThingFromBackend(param: string): Observable<any>{
        return this.httpClient.get(this.host + this.url+ "/" + param);
}
await this.http.get(...).toPromise(value => {
  ...
});