-2

I have following problem. I need to return value from REST server. Everything is okey, i am getting what i want, but there is a small problem. Asynchronous is not working and i have no idea why. I will show you my code and result by images.

Code

Title

And result in chrome

enter image description here

Why is it not working as i want to work? Of course, result should have been 1/2 then 3. Thanks for any help

js code

ngOnInit() {
    this.error = this.getBlockedText();
}
private getBlockedText(): string {
    let customTxt;
    this.proxy.core.getUISettings({}).then(res => {
            customTxt = res;
            console.log('1');
    }).catch(err => {
        console.log('2');
    })
    console.log('3');
    return customTxt;
}

2 Answers2

3

Asynchronous is not working

Yes, it is.

Why is it not working as i want to work?

Because you want it to work synchronously and it is asynchronous.

Of course, result should have been 1/2 then 3.

If you want the promise to resolve then show 1 then show 2 then show 3 … you have to put each of them (including the 3) in then() handlers.

At the moment, you've put the 3 straight after the code which starts the asynchronous function, so it gets run before the asynchronous function has finished.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • Also, I'd note that the promise itself should be returned instead of a value, which will get defined way too late – Salketer Jul 24 '17 at 09:38
-1

Please refer to this link if you have questions regarding async calls. Last part runs before the async calls so your issue

tejA
  • 11
  • 4