You create an array like const promises = [];
and push all the promises promises.push(fetch(...));
. After all functions are started and all promises are stored you can simply wait for await Promise.all(promises);
const promises = [];
seImages.forEach((seImage) => {
const se_image = {
image: seImage.image,
alt_text: seImage.alt_text,
landing_page: newLp.id,
csrfmiddlewaretoken: getCookie('csrftoken'),
};
const newSeImage = urlEncodedFormData(se_image);
promises.push(postFormData(
ui.urls.createSeImage(),
newSeImage
).then((resp) => console.log(resp)));
});
vpPoints.forEach((vpPoint) => {
const vp_point = {
point: vpPoint.point,
description: vpPoint.description,
landing_page: newLp.id,
csrfmiddlewaretoken: getCookie('csrftoken'),
};
const newVpPoint = urlEncodedFormData(vp_point);
promises.push(postFormData(
ui.urls.createVpPoint(),
newVpPoint
).then((resp) => console.log('VpPoint: ', resp)));
});
(async function () {
await Promise.all(promises);
// window.location.reload
})();
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…