A
A
Alexander2021-04-17 10:30:59
Node.js
Alexander, 2021-04-17 10:30:59

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

2 answer(s)
D
Dmitry Belyaev, 2021-04-17
@bingo347

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 () => { /* ... */ },
]);

Option 2:
async function series(tasks) {
    for (const task of tasks) {
        await task();
    }
}

series([
    async () => { /* ... */ },
    async () => { /* ... */ },
    async () => { /* ... */ },
    async () => { /* ... */ },
]);

D
davidnum95, 2021-04-17
@davidnum95

Here is the basic implementation of the queue

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question