【问题标题】:React Javascript promises with multiple functions chainReact Javascript Promise 与多个函数链
【发布时间】:2017-11-03 19:51:36
【问题描述】:

我有 3 个功能:

我的 API 文件中有一个:

updateAPIFunction: (id, label) => {
    return new Promise(resolve => {
      window.setTimeout(function () {
        resolve();
      }, 3000);
    });
  }

容器中的一个 React 组件:

updateParentFunction(id, label) {
    controller.updateAPIFunction(id, label).then(a => {
      return a;
    });
  }

最后是从父容器传递的无状态组件中的一个:

this.props.updateParentFunction(id, label).then(result => {
      console.log('done')
    })

我从我的无状态控制器调用函数,函数被执行(api调用没有问题),但我的无状态组件返回:

Cannot read property 'then' of undefined

我不完全确定如何执行此操作。 我的第二个函数也应该返回一个承诺吗?

我需要这样,因为无状态组件会被多个父组件重用,它们有不同的 API。

【问题讨论】:

  • 你没有返回来自updateParentFunction的承诺。
  • 为此更改:updateParentFunction(id, label) { return controller.updateAPIFunction(id, label).then(a => {

标签: javascript reactjs asynchronous promise


【解决方案1】:

您错过了updateParentFunction 中的return 关键字:

updateParentFunction(id, label) {
  return controller.updateAPIFunction(id, label).then(a => {
    return a;
  });
}

【讨论】:

  • 不敢相信我错过了。谢谢!
猜你喜欢
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 2023-03-11
  • 2020-02-18
  • 1970-01-01
  • 1970-01-01
  • 2019-01-15
  • 2021-05-31
相关资源
最近更新 更多