Javascript 是否可以在react native中调用构造函数内的异步函数?

Javascript 是否可以在react native中调用构造函数内的异步函数?,javascript,reactjs,react-native,asynchronous,Javascript,Reactjs,React Native,Asynchronous,我有一个名为async a() 那么,如何在构造函数中调用异步函数呢? 因为构造函数在componentDidMount函数之前运行 我需要确保我的async a()首先在构造函数中完成,然后执行componentDidMount中的所有方法。在componentDidMount中调用a(),如下所示: async componentDidMount() { await a(); otherFuncions() } otherFunctions()仅在a()完成后执

我有一个名为
async a()

那么,如何在构造函数中调用异步函数呢? 因为构造函数在componentDidMount函数之前运行

我需要确保我的
async a()
首先在构造函数中完成,然后执行componentDidMount中的所有方法。

在componentDidMount中调用
a()
,如下所示:

async componentDidMount() {
       await a();
       otherFuncions()
}

otherFunctions()
仅在
a()
完成后执行

异步构造函数是一种潜在的反模式,因为它不会导致预期的行为

异步副作用应该在安装组件后发生,因此在
componentDidMount
中发生,这就是它的作用。延迟组件的生命周期是不可能的,用这些术语来考虑它是不正确的

它应该像这样工作:

class Foo extends Component
  async a() {...}

  async componentDidMount() {
    await this.a();
  }

  render() {
    (conditionThatIsTrueWhenThereResult) ? (
      <div>...the result from a...</div>
    ) : (
      <div>fallback</div>
    );
  }
}
类Foo扩展组件
异步a(){…}
异步组件didmount(){
等着吧;
}
render(){
(结果是否正确的条件)(
…来自。。。
) : (
退路
);
}
}

如果有必要保持组件同步,
a
异步副作用应该移动到父组件,只有当
a
的结果准备好使用时,它才会呈现子组件。

您不能在构造函数内部执行,因为构造函数不能等待
等待
因此,您可以为要在
async a()
之后运行的所有进程使用另一个函数(例如
b()
)。你有两种选择:

1-使用
异步/等待

async componentDidMount() {  
    try {
      await a();  // it will wait here untill function a finishes
    } catch(err) {}

    b(); // after function a finished, this function will calls
}
2-使用
。最后

componentDidMount() {
    // in below line, function `a` will call, and when it finishes, the function inside `.finally` will be notified
    a().finally(() => {
        b(); // now your function `a` finished and you can call extra process in function `b`
    });
}

构造函数中的异步工作是否定的

通常,您可以使用呈现微调器并触发异步调用的组件。然后,可以在异步调用完成后更新此组件以呈现其他内容

另一个解决方案是将该逻辑推送到呈现该组件的人。父级应触发异步调用,并仅在异步调用之后呈现组件


正如其他人所建议的,您可以使用组件生命周期中的
componentDidMount
来处理此问题。

请参阅:

使用
不安全的组件willmount()
组件willmount()
或更好地阅读此内容
或者请搜索什么是构造函数。

大家好,我已经测试了你们回答的所有代码,但结果如下所示

async function a() {
    setTimeout(() => {
        console.log('A');
    }, 2000);
}

async function b() {
    setTimeout(() => {
        console.log('B')
    }, 1000);
}

function c() {
    console.log("C");
}


async function componentDidMount() {
    await a();
    await b();
    c();
}

componentDidMount();
输出: C B A


正如您在这里所说,A和B应该首先完成,然后是最后一个非异步函数。异步组件didmount(){}将有其自身的风险。我不建议将async设置为componentDidMount func,而且我需要在componentDidMount执行之前完成所有a()函数任务。我已经搜索过如何设置async函数componentDidMount,大多数人都不建议将async设置为函数componentDidMount不像您那样。请确保使用async componentDidMount()是一种好方法还是不好。大多数人都不建议使用async函数componentDidMount,因为它不像您所做的那样。-谁要么就是这样,要么就是在父组件中执行
a()
,并通过道具提供结果。这两种方式都可以接受。谢谢你的评论,我很抱歉我的英语不太好。我的意思是在回答我的问题时,你在componentDidMount功能之前放置了async关键字,我不知道这是否会影响性能问题天气。因为大多数互联网用户不建议在componentDidMount之前使用async关键字怀疑有很多人会强调这个想法,因为这不是什么大问题。无论如何,这个建议是错误的<代码>异步
不会影响性能,它会影响componentDidMount的返回值。这与
componentDidMount(){返回This.a()}
大致相同。该方法以这种方式返回承诺,这使得组件更易于测试和扩展。在使用承诺的任何地方返回链接承诺都是正确的,除非这会导致问题(在本例中,这不会导致问题)。嗨,我在一个简单的javascript文件中测试了这一点,但结果如下:异步函数a(){setTimeout(()=>{console.log('a');},2000);}异步函数b(){setTimeout(()=>{console.log('B')},1000);}函数c(){console.log('c”);}异步函数componentDidMount(){wait a();wait B();c();}组件didmount();C B请参考最后一个答案,因为在注释中,代码看起来不太好
a
B
立即解析。您没有在它们内部链接的承诺。如果您想模拟延迟,应该是
异步函数a(){wait new promise(resolve=>setTimeout(resolve),2000));console.log('a')}
。请参见仅当异步函数中有
wait
或返回
Promise
时,它们才会等待。在您的示例中,您刚刚设置了超时,并且您的函数
a
b
都将在超时完成之前解决