连锁承诺发挥作用

chain promise to function

我有一个功能

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())

但它 returns a 无法读取 属性 then of undefined。我的印象是这可行。我如何将函数链接到承诺?

它会导致此错误,因为 func 没有 return 承诺。如果这部分是一个承诺:

server.insertPatientSurveyQuestionToDataBase(Store.getPatientID(), SurveyNumber, Store.getPatientQuestion());

您需要 return 它在 func 内:

const func = () => {
    return server.insertPatientSurveyQuestionToDataBase(Store.getPatientID(), SurveyNumber, Store.getPatientQuestion());
};

那你就可以安心使用了:

func().then(response => promise());