ServerAction을 Promise.all 로 호출했는데, 동시에 수행이 되지 않더라.
이슈 / 해결
"use server";
export const actionCall = async () => {
const currentTime = new Date().getTime();
await Promise.all([action1(), action2()]);
return new Date().getTime() - currentTime;
};
const action1 = async () => {
return new Promise<void>((resolve) => {
setTimeout(() => {
console.log("action1");
resolve();
}, 1000);
});
};
const action2 = async () => {
return new Promise<void>((resolve) => {
setTimeout(() => {
console.log("action2");
resolve();
}, 1000);
});
};
export async function GET() {
const currentTime = new Date().getTime();
await Promise.all([action1(), action2()]);
const result = new Date().getTime() - currentTime;
return NextResponse.json({ result: result.toString() });
}
const handleApi3 = async () => {
const currentTime = new Date().getTime();
await Promise.all([
fetch("/api/call3").then((res) => res.json()),
fetch("/api/call3").then((res) => res.json()),
]);
const result = new Date().getTime() - currentTime;
setResult4(result.toString());
};
const handleApi4 = async () => {
const currentTime = new Date().getTime();
await Promise.all([
fetch("/api/call3").then((res) => res.json()),
fetch("/api/call4").then((res) => res.json()),
]);
const result = new Date().getTime() - currentTime;
setResult5(result.toString());
};
const handleApi3 = async () => {
const currentTime = new Date().getTime();
await Promise.all([
fetch("/api/call3?1").then((res) => res.json()),
fetch("/api/call3?2").then((res) => res.json()),
]);
const result = new Date().getTime() - currentTime;
setResult4(result.toString());
};
https://github.com/iamchiwon/serveraction_vs_api