Server Actions in next.js seem to be running in ‘series’

I have a next.js app which needs to make 5 requests to an external resource (only accessible on the server side), so from the ‘client’ component, when the user clicks a button, I use the async pattern to make the 5 calls, and deal with the response. However, I’m finding that even though I’m using promises, the server side seems to queue these server actions.

I’ve had a look through the documentation about server actions/functions, and don’t see any mention of there being a limit of one call at a time.

I have created a simple next.js app to demonstrate this.

serverFunctions.js

"use server"

// Wait for 5 seconds, only called on server
export async function ServerDelay(index) {
    console.log(`calling ${index} ` + new Date().toLocaleTimeString())
    return new Promise(resolve => {
        setInterval(() => {
            resolve({'index': index, 'time': new Date().toLocaleTimeString()})
        }, 5000)
    })
}

// Call both 5 second delays from the server
export async function launchOnServer() {
    ServerDelay(1).then(x => {console.log(x)})
    ServerDelay(2).then(x => {console.log(x)})
}

page.js

"use client"

import {ServerDelay, launchOnServer} from "./serverFunctions";

export async function ClientDelay(index) {
  console.log(`calling ${index} ` + new Date().toLocaleTimeString())
  return new Promise(resolve => {
    setInterval(() => {
      resolve({'index': index, 'time': new Date().toLocaleTimeString()})
    }, 5000)
  })
}

// Calls the 'delay' function on the server with two server requests
function launchSeparately() {
  ServerDelay(1).then(x => {console.log(x)})
  ServerDelay(2).then(x => {console.log(x)})
}

// Calls the delay locally on the client without touching the server-side component
function clientLaunch() {
  ClientDelay(1).then(x => {console.log(x)})
  ClientDelay(2).then(x => {console.log(x)})
}

// This makes a single 'server' request, which then makes the two calls from the server side
function launchBoth() {
  launchOnServer()
}

export default function Home() {
  return <div>
    <button onClick={launchSeparately}>Multiple Server Requests</button><br></br><br></br>
    <button onClick={launchBoth}>Single Server Requests</button><br></br><br></br>
    <button onClick={clientLaunch}>Client Request</button>
  </div>
}

If I click the Multiple Server Request button, the server terminal window shows the following…

calling 1 10:06:38
 POST / 200 in 5079ms
calling 2 10:06:43
 POST / 200 in 5033ms

And the browser console shows..

So, as you can see, the server doesn’t receive the 2nd call until the 1st is finished, and the whole process takes 10 seconds.

If I click either of the other buttons, i.e. both running on the client, or both running on the server, the Delays run at the same time, and return at the same time, taking just 5 seconds.

i.e. on the Single Server Request button, it shows this on the server..

calling 1 10:13:02
calling 2 10:13:02
 POST / 200 in 24ms
{ index: 1, time: '10:13:07' }
{ index: 2, time: '10:13:07' }

And similar when everything runs on the client

I realise that I can re-factor my code to pass 5 URLs or whatever to the server side, and control execution there, but I’m really hoping I’m just missing something, and there’s a setting somewhere to allow multiple server actions/functions to happen at the same time.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật