비동기를 사용하고 내 반응 js 앱을 기다리고 있습니다. 하지만 제대로 작동하지 않습니다.
const handleFinish = async () => {
const startDateString = startDate.format('YYYY-MM-DDThh:mm');
const endDateString = endDate.format('YYYY-MM-DDThh:mm');
const createdString = moment().format('YYYY-MM-DDThh:mm');
const requestData = [];
const previousData = [];
selectedCustomerData.forEach(customer => {
selectedProductData.forEach(product => {
previousData.push({
customer: customer,
product: product
});
});
});
await previousData.forEach(async element => {
const tempStartDate = moment().add(element.customer.leadtime, 'days');
const apiUrl = '/productprice/?customerid=' + element.customer.customerid +
'&productkey=' + element.product.productkey +
'&isrulendNull=true';
await api.get(apiUrl).then(response => {
let newPrice = 0;
if (priceMethod === 'fixed') {
newPrice = price;
} else if (priceMethod === 'specific') {
newPrice = response.data[0].productpriceusd + price;
} else if (priceMethod === 'relative') {
newPrice = response.data[0].productpriceusd % 100.0 * (100.0 + price);
}
requestData.push({
productkey: element.product.productkey,
productcode: element.product.productcode,
customerid: element.customer.customerid,
customerchain: element.customer.customerchain,
productpriceusd: newPrice,
rulestart: (tempStartDate > startDate) ? tempStartDate.format('YYYY-MM-DDThh:mm') : startDateString,
ruleend: endDateString,
creatorupn: user.data.displayName,
created: createdString,
customername: element.customer.customername,
productdescription: element.product.productdescription,
});
});
});
console.log(requestData);
setPricesList(requestData);
};
foreach 후에 requestData 배열을 예상했습니다. 그러나 해당 console.log는 foreach 후에 발생하지 않고 foreach 전에 발생합니다. 즉, async 및 await가 작동하지 않습니다. 내 코드에 무엇이 잘못 되었나요?
Array.forEach()
주어진 비동기 함수에서 모든 요소가 처리 될 때까지 기다리지 않습니다.
다음 for
과 같은 일반 루프 를 사용할 수 있습니다 .
const handleFinish = async () => {
const startDateString = startDate.format('YYYY-MM-DDThh:mm');
const endDateString = endDate.format('YYYY-MM-DDThh:mm');
const createdString = moment().format('YYYY-MM-DDThh:mm');
const requestData = [];
const previousData = [];
selectedCustomerData.forEach(customer => {
selectedProductData.forEach(product => {
previousData.push({
customer: customer,
product: product
});
});
});
for (const element of previousData) {
const tempStartDate = moment().add(element.customer.leadtime, 'days');
const apiUrl = '/productprice/?customerid=' + element.customer.customerid +
'&productkey=' + element.product.productkey +
'&isrulendNull=true';
await api.get(apiUrl).then(response => {
let newPrice = 0;
if (priceMethod === 'fixed') {
newPrice = price;
} else if (priceMethod === 'specific') {
newPrice = response.data[0].productpriceusd + price;
} else if (priceMethod === 'relative') {
newPrice = response.data[0].productpriceusd % 100.0 * (100.0 + price);
}
requestData.push({
productkey: element.product.productkey,
productcode: element.product.productcode,
customerid: element.customer.customerid,
customerchain: element.customer.customerchain,
productpriceusd: newPrice,
rulestart: (tempStartDate > startDate) ? tempStartDate.format('YYYY-MM-DDThh:mm') : startDateString,
ruleend: endDateString,
creatorupn: user.data.displayName,
created: createdString,
customername: element.customer.customername,
productdescription: element.product.productdescription,
});
});
}
console.log(requestData);
setPricesList(requestData);
};
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다