gpt4 book ai didi

javascript - 连接两个 promise

转载 作者:行者123 更新时间:2023-12-01 01:13:46 24 4
gpt4 key购买 nike

我有两个 promise

    const promise_1 = this.connection.insertPatientToDataBase(Store.getPotentialPatientID())
.then(ting => {
console.log(ting);
Dispatcher.dispatch({
actionType: Constants.CHANGE_POTENTIAL_PATIENT_PASSWORD,
payload: ting.data.password
})})
.catch(error => {console.log(error)});

const promise_2 = this.connection.getAllPatientData()
.then( function(response) {
console.log("Dispatrinc a new server call")
console.log(response.data)
Dispatcher.dispatch({
actionType: Constants.CHANGE_ALL_PATIENTS,
payload: response.data
})})
.catch(error => console.log(error))


console.log("Done");
}

第一个将向服务器发布一些数据,第二个查询数据以重新下载新名单。第二个依赖于第一个。问题是第一个 promise 之后就兑现了。第二个 promise 首先兑现。我怎样才能将这两个 promise 链接在一起那么 Promise 2 会等待 Promise 1 吗?

最佳答案

如果两个函数不相关,但promise_1必须首先解析以便患者存在,您可以将promise创建包装在一个函数中,并且仅在promise_1解析时调用promise_2创建:

const promise_1 = () => this.connection.insertPatientToDataBase(Store.getPotentialPatientID())
.then(ting => {
console.log(ting);
Dispatcher.dispatch({
actionType: Constants.CHANGE_POTENTIAL_PATIENT_PASSWORD,
payload: ting.data.password
})})
.catch(error => {console.log(error)});

const promise_2 = () => this.connection.getAllPatientData()
.then( function(response) {
console.log("Dispatrinc a new server call")
console.log(response.data)
Dispatcher.dispatch({
actionType: Constants.CHANGE_ALL_PATIENTS,
payload: response.data
})})
.catch(error => console.log(error));

promise_1().then( response => promise_2());

如果promise_2依赖于promise_1的结果来运行,例如,如果promise_1将返回患者id,并且您需要该id来运行promise_2,并且只有promise_2的结果必须在两者解析后可用,那么您可以修改上面一点点来传递参数:

const promise_1 = () => this.connection.insertPatientToDataBase(Store.getPotentialPatientID())
.then(ting => {
console.log(ting);
Dispatcher.dispatch({
actionType: Constants.CHANGE_POTENTIAL_PATIENT_PASSWORD,
payload: ting.data.password
})})
.catch(error => {console.log(error)});

const promise_2 = patient_id => this.connection.getAllPatientData( patient_id )
.then( function(response) {
console.log("Dispatrinc a new server call")
console.log(response.data)
Dispatcher.dispatch({
actionType: Constants.CHANGE_ALL_PATIENTS,
payload: response.data
})})
.catch(error => console.log(error));

promise_1()
.then( patient_id => promise_2( patient_id ))
.then( patient_data => {
// handle patient data.
});

您还可以将所有内容重组为更多的原子函数,因此每个 promise 都有一个特定的目标,因此您可以将它们全部链接在一起。如果您以不同的方式嵌套结构,您甚至可以保存所有响应并在最后返回所有 fo。

const create_patient_id = () => this.connection.insertPatientToDataBase(Store.getPotentialPatientID());

const create_patient = patient_id => Dispatcher.dispatch({
actionType: Constants.CHANGE_POTENTIAL_PATIENT_PASSWORD,
payload: patient_id.data.password
});

const get_patients = () => this.connection.getAllPatientData();

const update_patients = patients => Dispatcher.dispatch({
actionType: Constants.CHANGE_ALL_PATIENTS,
payload: patients.data
})

const workflow = () => create_patient_id()
.then( create_patient );
.then( get_patients )
.then( update_patients );

workflow();

关于javascript - 连接两个 promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54944306/

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