class Queue {
queue = [];
isRunning = false;
add(task) {
queue.push(task);
if (!this.isRunning) {
this.start();
}
}
async start() {
this.isRunning = true;
for (let i = 0; i < this.queue.length; i++) {
await this.queue[i]();
}
this.isRunning = false;
}
}
const queue = new Queue();
queue.add(() => {
return new Promise((resolve, reject) => {
setTimeout(() => {
//your timer
}, 1000)
});
})
router.post('/', async (ctx) => {
const user = await new Promise((resolve, reject) => {
passport.authenticate('local', (err, user) => {
if (err) return reject(err);
return resolve(user);
})
})
if (!user) {
ctx.render('/', { title: 'Авторизация', message: ctx.body });
} else {
const payload = {
id: user.id,
displayName: user.displayName,
email: user.email
};
const token = jwt.sign(payload, jwtsecret); //здесь создается JWT
ctx.body = { user: user.displayName, token: 'JWT ' + token };
ctx.redirect('/user');
}
});