Answer the question
In order to leave comments, you need to log in
How to make all requests wait for one promise?
Many users perform an action where I either need to pick up an object with a variable, and if it is not there, load it from the database and write it to this variable.
The problem is when many users quickly perform an action, I have duplicate requests to the database and the objects themselves too. How to make subsequent requests, after the first download request, wait for the first one, and not start loading themselves?
// внутри async function
chat.quiz = global_quizzes[quiz_id];
if(!chat.quiz) {
// проблема в дубли
chat.quiz = await Chat_Quizzes.findById(quiz_id).exec();
global_quizzes[quiz_id] = chat.quiz;
}
Answer the question
In order to leave comments, you need to log in
You can use queue
Example:
'use strict';
const { Queue } = require('plain-queue');
const queue = new Queue;
// внутри async function
chat.quiz = global_quizzes[quiz_id];
if(!chat.quiz) {
chat.quiz = await queue.addTask(function () {
return global_quizzes[quiz_id] || Chat_Quizzes.findById(quiz_id).exec();
});
global_quizzes[quiz_id] = chat.quiz;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question