【问题标题】:chain promise to function链承诺功能
【发布时间】:2019-03-01 14:59:45
【问题描述】:

我有一个功能

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?

【问题讨论】:

    标签: javascript promise chaining


    【解决方案1】:

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

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

    你需要在func里面返回它:

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

    那么你就可以像这样安全地使用它了:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-01
      • 2018-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多