5

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

no, please, no, don't do this. this is a terrible idea for any application running in the browser, as it will literally freeze the application while the request is in flight. See the MDN docs that mention this being a bad idea. developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.