我有某些功能,需要我进行一个 api 调用来检索第二个 api 调用中使用的某个 id,逻辑上它看起来像这样:
componentWillMount () {
this.props.functionOne()
// Wait for functionOne to complete before calling this
this.props.functionTwo(this.props.paramFromFunctionOne)
}
其中 this.props.paramFromFunctionOne
是函数一完成后存储在 redux 状态中的内容。
最佳答案
据我正确理解,您可以通过以下方式实现:
componentWillMount () {
// As `axios` returns promise, you should return it from `functionOne`
// This way you can be able to use `.then` method.
this.props.functionOne()
.then(() => {
// This code block would be executed after api call from first function will be finished.
this.props.functionTwo(this.props.paramFromFunctionOne)
});
}
关于javascript - 调用函数 "step by step"的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38895718/