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 - 当页面处于事件状态时更改类

javascript - 如何在 ReactJS 中查询字段值?

javascript - 如何在 React 中获取从状态生成的查询参数

express - 允许express将路由传递给react-router

react-native - 使用 Genymotion 模拟器在 Ubuntu 上运行 React-Native Android 应用程序

react-native - React 抽屉导航 v5x useNavigation() 与组件类

javascript - 应用程序切换后,iPad 显示网站/应用程序的错误状态

Internet Explorer 11 中的 JavaScript 错误

javascript - 将滚动条添加到不需要它的页面?

ios - React-native,如何获取文件 Assets 图像绝对路径?