sync-queues
Utility for synchronizing async code.
Usage
sync-queues has a very small surface API, you create synchronized queues and run tasks on it:
import syncQueue from "sync-queues";
const wait = (time: number) => new Promise((resolve) => setTimeout(resolve, time));
async function organizedSequence() {
const taskTimeout = 1000;
const q = syncQueue(taskTimeout);
const sequence: number[] = [];
const a = q.run(async() => {
for (let i = 0; i < 4; i ++) {
await wait(100);
sequence.push(i);
}
return 10;
});
const b = q.run(async() => {
for (let i = 4; i < 8; i ++) {
await wait(10);
sequence.push(i);
}
return 20;
});
const result = [await a, await b];
console.log(sequence);
return result;
}
async function start () {
const s = await organizedSequence();
console.log(s);
}
start();