Answer the question
In order to leave comments, you need to log in
Which tool to use for synchronous task queue?
I would like something unusual, from the point of view of the logic of asynchronous JS. Namely, some familiar task queue, in which each next one is executed after the completion of the previous one. Is there any solution out there for this?
Answer the question
In order to leave comments, you need to log in
So I understand the tasks themselves are asynchronous, but they must be performed sequentially.
The usual array of functions and promises is enough for the eyes:
Option 1:
function series(tasks) {
return tasks.reduce((p, task) =>
p.then(() =>
task()), Promise.resolve());
}
series([
async () => { /* ... */ },
async () => { /* ... */ },
async () => { /* ... */ },
async () => { /* ... */ },
]);
async function series(tasks) {
for (const task of tasks) {
await task();
}
}
series([
async () => { /* ... */ },
async () => { /* ... */ },
async () => { /* ... */ },
async () => { /* ... */ },
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question