gpt4 book ai didi

javascript - 链式 promise 发挥作用

转载 作者:行者123 更新时间:2023-12-03 00:06:19 28 4
gpt4 key购买 nike

我有一个函数

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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com