Angular2 http synchronous

Please help me make an example about http with synchronous in Angular2?

I tried as below: In component:

getAllAddress(){ this.addressService.getAllAddress().then( result => { this.data = result.list; this.onChangeTable(this.config, null); console.log('FIRST'); } ); console.log('LAST'); } 

In the service:

 public getAllAddress(){ return this.__http.get('LOCATION') .map((res) => { return res.json() }) .toPromise(); } 

But the console show log is "LAST" to "FIRST".

Thanks.

+5
source share
1 answer

You will need to create your own Connection and ConnectionBackend classes that will be implemented when the application loads. See code example below.

 export class XHRSynchronousConnection implements Connection { } export class XHRSynchronousConnectionBackend implements ConnectionBackend { } 

You can download it as follows

 bootstrap([provide(ConnectionBackend, {useClass:XHRSynchronousBackend}), provide(Connection,{useClass:XHRSynchronousConnection}]; 

You can see the rest of the code in the actual source code .

+1
source

Source: https://habr.com/ru/post/1239400/


All Articles