Junior — Middle+
Creating an AsyncQueue for guaranteed sequential execution of asynchronous functions
livecode
Task condition
It is necessary to develop a class that will execute the passed functions strictly one after another, even if they return a Promise.
const asyncQueue = new AsyncQueue();
asyncQueue.add(() => console.log("1"));
asyncQueue.add(
() =>
new Promise((resolve) => {
setTimeout(() => {
console.log("2");
resolve();
}, 1000);
})
);
asyncQueue.add(() => console.log("3"));
asyncQueue.add(
() =>
new Promise((resolve) => {
setTimeout(() => {
console.log("4");
resolve();
}, 500);
})
);