How to synchronise Angular2 http get?

后端 未结 8 1355
無奈伤痛
無奈伤痛 2021-01-07 19:27

I understand using observable I can execute a method when the request is completed, but how can i wait till a http get is completed and return the response using in ng2 http

8条回答
  •  长发绾君心
    2021-01-07 19:52

    I looked and I couldn't find any way to make an HTTP call sync instead of async.

    So the only way around this: wrap your call in a while loop with a flag. Don't let the code continue until that flag has "continue" value.

    Pseudo code as follows:

    let letsContinue = false;
    
    //Call your Async Function
    this.myAsyncFunc().subscribe(data => {
       letsContinue = true;
    }; 
    
    while (!letsContinue) {
       console.log('... log flooding.. while we wait..a setimeout might be better');
    }
    

提交回复
热议问题