Minimal asynchronous tasks solving queue with zero dependences
$> npm install https://github.com/adefrutoscasado/task-queue
// app.js
import TaskQueue from 'task-queue'
const updateDatabase = async id => {
const startTime = new Date()
await randomDelay()
const endTime = new Date()
console.log(
`update ${id}@${startTime.toISOString()} finished@${endTime.toISOString()}`
)
return { id }
}
const taskQueue = new TaskQueue()
taskQueue.enqueue(() => updateDatabase(1)) // returns {id: 1}
taskQueue.enqueue(() => updateDatabase(2)) // returns {id: 2}
The tasks are always solved on a first-come, first-served basis and never solved simultaneously:
update 1@2020-01-19T20:15:46.666Z finished@2020-01-19T20:15:47.459Z
update 2@2020-01-19T20:15:47.459Z finished@2020-01-19T20:15:47.476Z
MIT