javascript - 删除链式 promise

标签 javascript reactjs react-native chaining method-chaining

react-native 新手,目前我正在研究链式 promise 。

myFunction(human, destination = null) {
    const { navigation } = this.props;
    const { onRefresh } = navigation.state.params;
    this.setState({ isLoading: true });
    return PeopleService.closeService(
      human.humanId,
      destinationPoint && destinationPoint.humanId,
    )
      .then((result) => {
        if (result) {
          PeopleHelperService.refreshInfo().then(() => {
            if (onRefresh) {
              onRefresh();
            }
            navigation.popToTop();
            PopUp.showSuccess(
              "Success message",
            );
          });
        }
        PopUp.showError(
          "Failing message",
        );
        return null;
      })
      .finally(() => this.setState({ isLoading: false }));
  }

我想要实现的目标是消除链责任并使其变得简单而无需链接。

有人可以指导我如何实现这一目标吗?一些文档和其他来源的链接对于我了解如何制作它非常有帮助。

更新: 似乎是异步/等待工作的答案。

最佳答案

如果您不想使用 Promise,那么可以使用 async wait。在这里。

myFunction = async (human, destination = null) => {
    const { navigation } = this.props;
    const { onRefresh } = navigation.state.params;
    this.setState({ isLoading: true });
    let result = await PeopleService.closeService(
      human.humanId,
      destinationPoint && destinationPoint.humanId,
    );

    if (result) {
        await PeopleHelperService.refreshInfo();
        if (onRefresh) {
            onRefresh();
        }
        navigation.popToTop();
        PopUp.showSuccess(
            "Success message",
        );
    }
    PopUp.showError(
        "Failing message",
    );
    this.setState({ isLoading: false })
}

关于javascript - 删除链式 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57321651/

相关文章:

javascript - 堆栈导航器给我未定义的错误

reactjs - 如何在 React JS 中将 const 的值从一个 .js 文件传递​​到另一个文件?

javascript - 如何将数据作为导航 Prop 传递(不起作用)?

react-native - react native :after styling like css

javascript - "and ' 之间的区别

javascript - 在javascript中将毫秒字符串转换为日期

reactjs - 使用 moment 时 DatePicker 上的 React Moment 问题(可能依赖)

javascript - Javascript 库中的国际化 (i18n)

javascript - 从默认路由重定向到另一条路由时出现滞后

android - React-native - 应用程序在模拟器上工作但不在真实设备上