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

Time:07-17

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?

CodePudding user response:

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

  • Related