繁体   English   中英

如何在MobX操作中包括多个等待呼叫

[英]How to include multiple await calls in MobX action

我正在开发一个需要在MobX动作中使用await语法进行多次调用的应用程序。 一个典型的示例如下所示:

  @action.bound
  async mintToken() {
    const tokenInstance = await this.token.deployed();    
    await tokenInstance.mint(1, { from: this.account });
    await this.updateLastMintedToken();
    await this.updateTokenDetails();
  }

但是,由于MobX处理动作的方式,该代码似乎无法正确执行。 根据文档

@action仅适用于代码块,直到第一次等待。 并且在每次等待之后启动一个新的异步函数,因此在每次等待之后,状态修改代码应包装为动作。

docs中给出的示例使用runAsAction ,我在以下代码中尝试过:

  @action.bound
  async updateLastMintedToken() {
    const tokenInstance = await this.token.deployed();

    runInAction(async function() {
      const result = await tokenInstance.lastTokenMinted.call();
      this.lastTokenMinted = result.toNumber();
    });

  }

不幸的是,这没有用,因为在runInAction调用中this变得不确定,因此我无法runInAction状态。

非常感谢在一个操作中运行多个异步调用的任何方向!

您可以使用箭头函数将runInAction中的上下文设置为与updateLastMintedToken函数相同的上下文

  @action.bound
  async updateLastMintedToken() {
    const tokenInstance = await this.token.deployed();

    runInAction(async () => {
      const result = await tokenInstance.lastTokenMinted.call();
      this.lastTokenMinted = result.toNumber();
    });

  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM