我有一個帶有快速一條路線的 API,花一些時間來獲取所需的所有資料(通過長 JSON 物件搜索)
router.get(
"/:server/:maxCraftPrice/:minBenef/:from/:to",
checkJwt,
async (req, res) => {
const getAllAstuces = new Promise(async (resolve, reject) => {
const { EQUIPMENTS_DIR, RESOURCES_DIR } = paths[req.params.server];
const astuces = [];
const { from, to, maxCraftPrice, minBenef } = req.params;
const filteredEquipments = getItemByLevel(from, to);
for (const equipment in filteredEquipments) {
// parsing and push to astuces array
}
resolve(astuces);
});
const resource = await getAllAstuces;
return res.json(resource);
}
);
現在在我的網站上,當有人訪問與此路由關聯的頁面時,正在加載資料時,所有其他請求都像在佇列中一樣被鎖定
我試圖添加 Promise 來處理這個但沒有改變
有沒有辦法同時處理請求,或者我應該重構該路由以使其更快?
uj5u.com熱心網友回復:
如果您的請求需要很長時間來處理,它將阻止所有其他請求,直到完成。如果您可以使請求花費更少的處理時間,這是一個很好的起點,但您可能需要采取進一步的步驟來更快地處理多個請求。
有多種方法可以解決這種情況。本文介紹了幾種方法。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/410854.html
標籤:
上一篇:從同步代碼呼叫異步I/O方法
