Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 http synchronous

Tags:

angular

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 service:

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

But the console show log is 'LAST' before 'FIRST'.

Thanks.

like image 722
Nguyen Avatar asked Nov 09 '22 22:11

Nguyen


1 Answers

You will have to create your own implementations Connection and ConnectionBackend class and inject it while bootstrapping your app. See sample code below

export class XHRSynchronousConnection implements Connection    
 {

 }

export class XHRSynchronousConnectionBackend implements ConnectionBackend
{
}

You can bootstrap it as follows

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

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

like image 88
Jigar Avatar answered Nov 15 '22 07:11

Jigar