从 promise 调用另一个 promise 访问数据
Access data from promise calling another promise
我有一个调用另一个 promise 的 promise,但我不知道如何访问我试图存储所有 promise 的变量 memberContractInfo
。在下面的代码中,我有 2 个标记为 QUESTION 1
和 QUESTION 2
.
的问题
export function sendRequestAndLoadResponseForAllMemberContractInfo() {
return function sendRequestAndLoadResponseForAllMemberContractInfoThunk(dispatch) {
dispatch(getRequestsAction());
return returnPromiseWithAllMemberContracts()
.then(promiseWithAllMemberContracts => {
// Step 1) get all member ids in response
let contracts = promiseWithAllMemberContracts.response.contract;
let memberContractInfo = []; // <==== I want to store result of all 2nd promises here
for (let i in contracts) {
const memberID = contracts[i].member_id;
returnPromiseWithAllMemberInfo(memberID)
.then(secondAPICallResponse => {
// Step 2) make 2nd API call using memberIDs as parameter
memberContractInfo.push(secondAPICallResponse);
console.log('secondAPICallResponse = ', secondAPICallResponse);
if (memberContractInfo.length === 2) {
console.log('memberContractInfo.length = 2');
// QUESTION 1: I can access memberContractInfo here but I there must also be
// another place I can access it right?
}
})
}
console.log('memberContractInfo = ', memberContractInfo); // <== QUESTION 2: Why is this empty?
});
}
}
function returnPromiseWithAllMemberContracts() {
return fetchData('/api-proxy/contract/contract');
}
function returnPromiseWithAllMemberInfo(memberID) {
let servicePath = '/api-proxy/member?id='.concat(memberID);
console.log('fetchData(', servicePath);
return fetchData(servicePath);
}
您可以在 then(promiseWithAllMemberContracts => {}
声明的范围内的任何地方访问 memberContractInfo
。
memberContractInfo
在 console.log('memberContractInfo = ', memberContractInfo);
中是空的,因为您在实际解决 promise 之前到达此语句。
如@bergi 所述,您需要使用 Promise.all
而不是循环。
Promise.all(contracts.map((contract) => {
return returnPromiseWithAllMemberInfo(contract.member_id);
})).then(values => {
// values has response from all the above API calls stored in it
return values;
}).then((memberContractInfo) => {
console.log(memberContractInfo.length);
// this will give you correct value.
})
我有一个调用另一个 promise 的 promise,但我不知道如何访问我试图存储所有 promise 的变量 memberContractInfo
。在下面的代码中,我有 2 个标记为 QUESTION 1
和 QUESTION 2
.
export function sendRequestAndLoadResponseForAllMemberContractInfo() {
return function sendRequestAndLoadResponseForAllMemberContractInfoThunk(dispatch) {
dispatch(getRequestsAction());
return returnPromiseWithAllMemberContracts()
.then(promiseWithAllMemberContracts => {
// Step 1) get all member ids in response
let contracts = promiseWithAllMemberContracts.response.contract;
let memberContractInfo = []; // <==== I want to store result of all 2nd promises here
for (let i in contracts) {
const memberID = contracts[i].member_id;
returnPromiseWithAllMemberInfo(memberID)
.then(secondAPICallResponse => {
// Step 2) make 2nd API call using memberIDs as parameter
memberContractInfo.push(secondAPICallResponse);
console.log('secondAPICallResponse = ', secondAPICallResponse);
if (memberContractInfo.length === 2) {
console.log('memberContractInfo.length = 2');
// QUESTION 1: I can access memberContractInfo here but I there must also be
// another place I can access it right?
}
})
}
console.log('memberContractInfo = ', memberContractInfo); // <== QUESTION 2: Why is this empty?
});
}
}
function returnPromiseWithAllMemberContracts() {
return fetchData('/api-proxy/contract/contract');
}
function returnPromiseWithAllMemberInfo(memberID) {
let servicePath = '/api-proxy/member?id='.concat(memberID);
console.log('fetchData(', servicePath);
return fetchData(servicePath);
}
您可以在
then(promiseWithAllMemberContracts => {}
声明的范围内的任何地方访问memberContractInfo
。memberContractInfo
在console.log('memberContractInfo = ', memberContractInfo);
中是空的,因为您在实际解决 promise 之前到达此语句。
如@bergi 所述,您需要使用 Promise.all
而不是循环。
Promise.all(contracts.map((contract) => {
return returnPromiseWithAllMemberInfo(contract.member_id);
})).then(values => {
// values has response from all the above API calls stored in it
return values;
}).then((memberContractInfo) => {
console.log(memberContractInfo.length);
// this will give you correct value.
})