我有一个函数
const func = () => server.insertPatientSurveyQuestionToDataBase(Store.getPatientID(), SurveyNumber, Store.getPatientQuestion())
这就是所谓的。
在此之后,有一个函数:
const promise =
server.GetPatientHistoryData(Store.getPatientID())
.then(
response => Dispatcher.dispatch({
actionType: Constants.CHANGE_PATIENT_HISTORY,
payload:response}))
.catch(error => {console.log(error)});
我这样做,我相信这应该有效:
func().then(response => promise())
但它返回一个无法读取的未定义属性。我的印象是这可行。如何将函数链接到 Promise?
最佳答案
它会导致此错误,因为 func
不返回 promise 。如果这部分是一个 promise :
server.insertPatientSurveyQuestionToDataBase(Store.getPatientID(), SurveyNumber, Store.getPatientQuestion());
您需要将其返回到func
内部:
const func = () => {
return server.insertPatientSurveyQuestionToDataBase(Store.getPatientID(), SurveyNumber, Store.getPatientQuestion());
};
然后你就可以像这样安全地使用它:
func().then(response => promise());
关于javascript - 链式 promise 发挥作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54947177/