两次单击后为什么要获取值? +承诺 [英] Why getting the value after clicking it twice? + Promise

查看:72
本文介绍了两次单击后为什么要获取值? +承诺的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在处理angular中的promise函数时,我缺乏对异步操作的理解.因此,基本上,我正在尝试从promise方法中获取一个值并将其分配给组件中的全局变量.但是,当我单击一次按钮时,我无法检索该值,而当我再次单击按钮时,它终于开始显示该值.

I have a lack of understanding asynchronous operation while I'm dealing with a promise function in angular. So basically, I'm trying to get a value from a promise method and assign it to a global variable in a component. However, I am unable to retrieve the value when I click on a button once and it finally starts to show the value after I clicked on a button once more.

单击一次时出现错误:

Cannot read property 'matchId' of undefined

点击后获得的价值两次:

3

HTML:

<button type="submit" (click)="loadInfo(form)">Submit</button>

服务:

@Injectable()
export class Web3Service {
    constructor...

    getInfo(address: string): Promise<any> {
        return this.TestBetting.deployed().then((instance) => {
          return instance.getInfo.call(address);
        })
        .then((value) => {
          var serialized = this.itemsService.getSerialized<IGetInfo>(value); 
          return this.mappingService.mapValueToGetInfo(serialized); 
        })
        .catch((e) => {
          console.log(e);
        });
    }  
}

组件:

export class HomeComponent {
    infoMapped: any;

    constructor(private web3Service: Web3Service) {}

    loadInfo(): void {
        var test = this.web3Service.getInfo(this.address); 
        test.then((value) => {
            this.infoMapped = value;
        })

        // this.infoMapped.matchId is undefined on a first attempt
        var length = this.infoMapped.matchId;
        for (var i = 0; i < length; i++) {
        //...
        }
    }
}

需要解决哪些问题,以便仅单击一次按钮即可检索到infoMapped值?

What needs to be fixed so that infoMapped value can be retrieved after clicking a button only once?

推荐答案

问题仍然是 test.then()(即for)之后的代码会在之前执行 this.infoMapped = value;,因为this.infoMapped = value;仅在test承诺解决后执行,并且只会在 运行后的某个时间解决.

The problem is still that code after the test.then() (i.e. the for) will execute before the this.infoMapped = value; because this.infoMapped = value; will only execute when the test promise resolves, and it will only resolve some time after the for runs.

我的建议:将处理"逻辑移至新方法,然后从.then()调用它.

What I suggest: move the "handling" logic to a new method and call it from the .then().

所以,这个:

loadInfo(): void {
    var test = this.web3Service.getInfo(this.address); 
    test.then((value) => {
        this.infoMapped = value;
    })

    // this.infoMapped.matchId is undefined on a first attempt
    var length = this.infoMapped.matchId;
    for (var i = 0; i < length; i++) {
    //...
    }
}

会变成这样:

loadInfo(): void {
    var test = this.web3Service.getInfo(this.address); 
    test.then((value) => {                 // <=== changed this line
        this.handleInfo(value)             // <=== changed this line
     });
}

handleInfo(value): void {                   // <=== added this whole method.
    this.infoMapped = value;

    var length = this.infoMapped.matchId;
    for (var i = 0; i < length; i++) {
    //...
    }
}

这篇关于两次单击后为什么要获取值? +承诺的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆