Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type 'Promise<string>' is not assignable to type 'string'

How can I put in variable response from service and then display it on hover? I tried this:

toolTip: string;

async mouseover(params) {
    this.handle = setTimeout(() => {
        this.toolTip = this.checkSaldo(10, 'FRA');
        this.show = true;
    }, 4000);

}

checkSaldo(amount: any, currencyCode: any): Promise<string> {
    return new Promise((resolve) => {
        this.restService.getByParam('recalculatedSaldo', { amount: amount, currency: currencyCode }).subscribe(response => {
            resolve(response.payload);
        });
    })

}

but I'm getting an error on toolTip variable:

Type 'Promise' is not assignable to type 'string'

Any suggestion?

like image 503
None Avatar asked Nov 29 '25 01:11

None


1 Answers

You can't assign the Promise into a string field.

But, you need to wait the Promise callback and assign the value as below:

this.checkSaldo(10, 'FRA').then((x) => {
    this.toolTip = x;
    ...
});

Sample StackBlitz Demo

like image 137
Yong Shun Avatar answered Dec 02 '25 05:12

Yong Shun



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!