adefrutoscasado / task-queue

Minimal asynchronous task solving queue with zero dependences

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Task-Queue

Minimal asynchronous tasks solving queue with zero dependences

Usage

$> 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 

License

MIT

About

Minimal asynchronous task solving queue with zero dependences

License:MIT License


Languages

Language:TypeScript 57.7%Language:JavaScript 42.3%