A lightweight async task queue management library
To install:
npm i @skapoor8/teak
Use in node.js:
const TaskQueue = require("@skapoor8/teak"); function main() { var tq = new TaskQueue(); var wait = (ms) => new Promise((resolve, reject) => { setTimeout(() => resolve(ms), ms); }); console.log("Start time:", new Date()); tq.enqueue(wait, [1000]).then((res) => console.log(`${res} seconds passed. Current time:`, new Date()) ); tq.enqueue(wait, [5000]).then((res) => console.log(`${res} seconds passed. Current time:`, new Date()) ); tq.enqueue(wait, [10000]).then((res) => { console.log(`${res} seconds passed. Current time:`, new Date()); tq.enqueue(wait, [1000]).then((res) => console.log(`${res} seconds passed. Current time:`, new Date()) ); }); } main();
Output in console:
Start time: 2021-07-03T21:04:53.536Z
1000 seconds passed. Current time: 2021-07-03T21:04:54.548Z
5000 seconds passed. Current time: 2021-07-03T21:04:59.551Z
10000 seconds passed. Current time: 2021-07-03T21:05:09.552Z
1000 seconds passed. Current time: 2021-07-03T21:05:10.556Z
Typically async tasks like the one show above can be performed in order by simply using async-await. This is a little trickier on the server side when tasks may be enqueued in response to a http request, and therefore we cannot use the await keyword or chain callbacks to ensure tasks are performed in order. Teak.js can be used in such situations.
Specifically, I wrote teak.js when I was working on an embedded database, and wanted to queue sequential writes to the same record coming from different requests.