我在Javascript中链接异步代码时遇到了问题
我正在尝试执行allCountryData并返回一个promise,它工作正常,但在allCountryData执行完毕后,我想对返回的数据/或allCountryDataArray执行一个操作,并将最高值存储在arrayOfHighestCases中 注意,我无法在allCountryData中链接其他登录名 如果你需要更多的细节,请告诉我我在Javascript中链接异步代码时遇到了问题,javascript,arrays,asynchronous,promise,Javascript,Arrays,Asynchronous,Promise,我正在尝试执行allCountryData并返回一个promise,它工作正常,但在allCountryData执行完毕后,我想对返回的数据/或allCountryDataArray执行一个操作,并将最高值存储在arrayOfHighestCases中 注意,我无法在allCountryData中链接其他登录名 如果你需要更多的细节,请告诉我 export const allCountryDataArray = []; export const arrayOfHighestCases = []
export const allCountryDataArray = [];
export const arrayOfHighestCases = [];
const allCountryData = async () => {
sendHTTP()
.then((res) => {
return res.response;
})
.then((res) => {
allCountryDataArray.push(...res);
return allCountryDataArray;
});
return await allCountryDataArray;
// Highest Cases
};
下面的代码无效
const highestCasesData = async () => {
// const allCountryDataArrayy = await allCountryData();
// allCountryData()
// .then((data) => {
// console.log(arrayOfHighestCases[0]);
// })
// .then((res) => {
const np = new Promise((res, rej) => {
res(allCountryData());
});
return np.then((res) => {
console.log(res);
const arrayofHigh = allCountryDataArray.sort((a, b) => {
if (a.cases.total < b.cases.total) {
return 1;
} else if (a.cases.total > b.cases.total) {
return -1;
} else {
return 0;
}
});
console.log(arrayofHigh);
const slicedArray = arrayofHigh.slice(0, 6);
for (const eachHighCase of slicedArray) {
arrayOfHighestCases.push(eachHighCase);
}
console.log(arrayOfHighestCases);
return arrayOfHighestCases;
});
// });
};
highestCasesData();
const highestCasesData=async()=>{
//const allCountryDataArrayy=等待allCountryData();
//allCountryData()
//。然后((数据)=>{
//console.log(arrayOfHighestCases[0]);
// })
//。然后((res)=>{
const np=新承诺((res,rej)=>{
res(allCountryData());
});
返回np.then((res)=>{
控制台日志(res);
const arrayofHigh=allCountryDataArray.sort((a,b)=>{
如果(a.病例总数b.cases.total){
返回-1;
}否则{
返回0;
}
});
控制台日志(arrayofHigh);
常数切片Darray=阵列高切片(0,6);
对于(切片Darray的每个实例){
阵列高盒推送(每个高盒);
}
控制台日志(arrayOfHighestCases);
返回arrayOfHighestCases;
});
// });
};
最高案例数据();
这很有效。如果我能做更多的改进,请告诉我
const allCountryData = async () => {
return sendHTTP()
.then((res) => {
return res.response;
})
.then((res) => {
allCountryDataArray.push(...res);
return allCountryDataArray;
});
// Highest Cases
};
const highestCasesData = async () => {
return allCountryData().then((res) => {
console.log(res);
const arrayofHigh = allCountryDataArray.sort((a, b) => {
if (a.cases.total < b.cases.total) {
return 1;
} else if (a.cases.total > b.cases.total) {
return -1;
} else {
return 0;
}
});
console.log(arrayofHigh);
const slicedArray = arrayofHigh.slice(0, 6);
for (const eachHighCase of slicedArray) {
arrayOfHighestCases.push(eachHighCase);
}
console.log(arrayOfHighestCases);
return arrayOfHighestCases;
});
};
highestCasesData();
const allCountryData=async()=>{
返回sendHTTP()
。然后((res)=>{
返回res.response;
})
。然后((res)=>{
allCountryDataArray.push(…res);
返回allCountryDataArray;
});
//最高案件
};
const highestCasesData=async()=>{
返回allCountryData()。然后((res)=>{
控制台日志(res);
const arrayofHigh=allCountryDataArray.sort((a,b)=>{
如果(a.病例总数b.cases.total){
返回-1;
}否则{
返回0;
}
});
控制台日志(arrayofHigh);
常数切片Darray=阵列高切片(0,6);
对于(切片Darray的每个实例){
阵列高盒推送(每个高盒);
}
控制台日志(arrayOfHighestCases);
返回arrayOfHighestCases;
});
};
最高案例数据();
用异步数据填充全局数组会导致计时冲突。数据不存在的bug,除了当您查看它存在的时候,还有一个关于“为什么我的代码不能访问数据?当我在控制台中检查时,一切看起来都很好,但我的代码不工作”的问题
如果要存储某些内容,请存储这些数组或函数的承诺
const allCountryData = async () => {
const res = await sendHTTP();
return res.response;
};
const highestCasesData = async () => {
const allCountryDataArray = await allCountryData();
return allCountryDataArray
.slice() // make a copy, don't mutate the original array
.sort((a, b) => b.cases.total - a.cases.total) // sort it by total cases DESC
.slice(0, 6); // take the first 6 items with the highest total cases
}
return wait allCountryDataArray
没有意义:allCountryDataArray
不是承诺。以sendHTTP
开头的承诺链将被丢弃,return
将在sendHTTP
等完成之前执行。您的意思是返回sendHTTP(
…)
…?您的np
也是无意义的,如果修复,allCountryData()
将返回一个承诺,因此不必将其封装在一个承诺中。也没有必要在传递给的函数中等待某些内容。然后()
,只要返回承诺就行了。明白了,我删除了np并返回了sendHTTP它现在可以工作了非常感谢您的帮助仅当您使用等待而不是然后时才使用异步